简体   繁体   中英

PowerShell script to store passwords fails

$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

Am using the above code to store the password in the text file c:\\pass.txt without having to reenter the password mutliple times.

I see the below error while trying this code:

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

Is the single quote at the very beginning of the script correct or a typo? because that could really mess things up... I was able to run the below code, and it works for me:

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

If you were running it from PowerShell and you forgot to set the $CredsFile variable, then you get that error. For ex. the below creates the same error:

$FileExists = Test-Path $Nothing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM