0
我创建了一个PowerShell脚本,用于在没有问题的情况下正常运行。现在暑假结束后,每当我运行脚本时,它都会要求凭证创建PSDrive。PowerShell未使用提供的凭据
$Source_Path = "\\192.168.1.32\Backup_Servers\EAVSRV01\WindowsImageBackup"
$Source_Username = "bcktest"
$Source_Password = "*****" | ConvertTo-SecureString -AsPlainText -Force
$Destination_Path = "\\192.168.1.34\Backup_Servers\EAVSRV01\Daily\"
$Destination_Username = "bcktest"
$Destination_Password = "*****" | ConvertTo-SecureString -AsPlainText -Force
#Creates the PSCredential, so we can login to servers, with the required credentials
$Source_Credentials = New-Object System.Management.Automation.PSCredential($Source_Username,$Source_Password)
$Destination_Credentials = New-Object System.Management.Automation.PSCredential($Destination_Username,$Destination_Password)
#Mount the paths as PSDrives, so they're easier to work with
New-PSDrive -Name DestinationDrive -PSProvider FileSystem -Root $Destination_Path -Credential $Destination_Credential
New-PSDrive -Name SourceDrive -PSProvider FileSystem -Root $Source_Path -Credential $Source_Credentials
当我尝试创建PSDrive“DestinationDrive”时,它会提示输入用户名和密码。如果我按取消,它仍然创建PSDrive。 当我尝试创建PSDrive“SourceDrive”时,它不会提示输入用户名或密码。它也创建PSDrive。 这里的重要部分是它不提示用户输入任何内容。
需要注意的事项:我不会在您的文件中以明文形式留下密码。如果这是在一个系统上运行(正如我怀疑的那样),使用'Read-Host -AsSecureString | ConvertFrom-SecureString'并将该输出分配给'$ Destination_Password = output |的ConvertTo-SecureString'。我将发布一个我用作答案的例子。 – TheIncorrigible1