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