2013-12-12 2 views
0
$Computers = get-content C:\temp\computers.txt 
$AdminName = Read-Host "Enter your Admin AD username" 
$CredsFile = "C:\pass.txt" 
$FileExists = Test-Path $CredsFile 
if ($FileExists -eq $false) { 
Write-Host 'Credential file not found. Enter your password:' -ForegroundColor Red 
Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File $CredsFile 
$password = get-content $CredsFile | convertto-securestring 
$Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist  $AdminName,$password} 
else 
{Write-Host 'Using your stored credential file' -ForegroundColor Green 
$password = get-content $CredsFile | convertto-securestring 
$Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName,$password} 
foreach ($computer in $Computers){ 
if(Test-Connection -ComputerName $Computer -Count 1 -ea 0)`` 
{ 
$network = Get-WmiObject win32_networkadapterconfiguration -ComputerName $Computer -Filter ipenabled="true" 
$ipaddress = $Network.IpAddress[0] 
$subnetmask = $Network.IPSubnet[0] 
    $DefaultGateway = $Network.DefaultIPGateway 
    $DNSServers = $Network.DNSServerSearchOrder 
$MACAddress = $Network.MACAddress 
$OutputObj = New-Object -Type PSObject 
    $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()  
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress 
    $OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask 
    $OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value $DefaultGateway 
    $OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value $DNSServers 
    $OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress 
    $Outputobj 
    $result += $Outputobj 
} 
} 
$result | Export-Csv c:\TEMP\Computerdata.csv 

암호를 여러 번 입력하지 않고 암호를 c : \ pass.txt 텍스트 파일에 저장하려면 위 코드를 사용하십시오.암호를 저장하는 PowerShell 스크립트가 실패합니다.

PS C:\temp> .\passcerds.ps1 Using your stored credential file ConvertTo-SecureString : Cannot process argument because the value of argument "input" is invalid. Change the value o the "input" argument and run the operation again. At C:\temp\passcerds.ps1:12 char:64 + $password = get-content $CredsFile | convertto-securestring <<<< + CategoryInfo : InvalidArgument: (:) [ConvertTo-SecureString], PSArgumentException + FullyQualifiedErrorId : ImportSecureString_InvalidArgument,Microsoft.PowerShell.Commands.ConvertToSecureStringC mmand

New-Object : Exception calling ".ctor" with "2" argument(s): "Cannot process argument because the value of argument " ssword" is null. Change the value of argument "password" to a non-null value." At C:\temp\passcerds.ps1:13 char:23 + $Cred = new-object <<<< -typename System.Management.Automation.PSCredential -argumentlist domain\$AdminName,$p sword} + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

답변

0

올바른 스크립트의 시작 또는 오타에 작은 따옴표가이 코드를하는 동안

나는 아래의 오류가 보이십니까? 당신이 $ CredsFile 변수를 설정하는 것을 잊었다

$CredsFile = "C:\pass.txt" 
$FileExists = Test-Path $CredsFile 

당신이 PowerShell을에서 그것을 실행중인 경우와, 다음 : 그 수 정말 엉망 일까지 ... 나는 아래의 코드를 실행할 수 있었고, 나를 위해 작동하기 때문에 그 오류가 발생합니다. 예를 들어. 아래에서 동일한 오류가 발생합니다.

$FileExists = Test-Path $Nothing 
+0

작은 따옴표는 오타입니다. 나는 $ CredsFile 변수를 선언합니다. 어떻게 그리고 어디서 정확히 그 일을하는지. PowerShell을 처음 사용하는 분이라면, 나를 지적 해 주시면 감사하겠습니다. 고맙습니다. – user3096445

+0

그게 나를 위해 일하는 thats. 한번 Powershell에서 실행하면 작동합니다. 고맙습니다. 이번에는 다른 오류가 나타납니다 – user3096445

+0

C : \ pass.txt 파일을 삭제 해보십시오. 파일이 비어 있거나 다른 종류의 데이터가 있으면 작동하지 않습니다. – HAL9256

관련 문제