我有一个通过UNCpath连接到远程服务器的PowerShell v1脚本。 重新启动后,需要进行身份validation,因为Windows显然不会记住,所以脚本无法连接到远程服务器
这种情况如何在PowerShell脚本中以编程方式处理?
我需要
1)重新authentication
2)通过UNCpath连接到远程服务器
也许“净”命令?
我怎样才能在我的PowerShell脚本中做到这一点?
Get-ChildItem -Path "\\REMOTESERVER\Data\Files" -Filter "*.journal" | Where-Object { $_.Name -match 'Daily_Reviews\[\d{1,12}-\d{1,12}\].journal' } | Sort-Object -Property CreationTime | ForEach-Object { $sourcefile = $_.Name [...] }
谢谢
您可以在脚本中存储凭据。 然后,您可以将PSCredential对象与New-PSDrive cmdlet一起使用来连接到共享。 PSDriveInfo对象在会话期间可以被脚本访问。
$username = 'domain\username' $password = 'secret' $password = $password | ConvertTo-SecureString -AsPlainText -Force $credential = New-Object System.Management.Automation.PSCredential($username, $password) New-PSDrive -Name journals -PSProvider FileSystem -Root '\\remoteserver\data\files' -Credential $credential | ForEach-Object { Set-Location "$_`:" } Get-ChildItem -Filter "*.journal"