当我尝试使用PSCredentials通过Powershell自动登录到我的服务器上的共享时,我感到困惑。
这是我目前使用的代码,没有使用PSCredentials...
#Login to server to copy installer files to desktop
Remove-PSDrive P
New-PSDrive -Name P -PSProvider FileSystem -Root \\192.168.1.85\Users2\Ross\Documents\Powershell -Credential Ross
#Copies installer files from server to the local desktop
Copy-Item -Path \\192.168.1.85\Users2\Ross\Documents\Powershell\ccsetup502.exe -Destination C:\Users\Ross\Desktop
#Executes copied installers
Start-Process C:\Users\Ross\Desktop\ccsetup502.exe -ArgumentList "/S" -Wait -Verb RunAs
#Deletes leftover installer files
Remove-Item C:\Users\Ross\Desktop\ccsetup502.exe这是我用来提供帮助的网站,但无论我用哪种方法将它应用到我自己的脚本中,它都不会起作用?
http://geekswithblogs.net/Lance/archive/2007/02/16/106518.aspx
提前感谢!
罗斯
发布于 2015-02-04 09:44:15
尝尝这个。它将提示您输入凭证,但如果您愿意,也可以创建它们并将其存储在变量中。
#Login to server to copy installer files to desktop
Remove-PSDrive P
New-PSDrive -Name P -PSProvider FileSystem -Root \\192.168.1.85\Users2\Ross\Documents\Powershell -Credential (Get-Credential)
#Copies installer files from server to the local desktop
Copy-Item -Path \\192.168.1.85\Users2\Ross\Documents\Powershell\ccsetup502.exe -Destination C:\Users\Ross\Desktop
#Executes copied installers
Start-Process C:\Users\Ross\Desktop\ccsetup502.exe -ArgumentList "/S" -Wait -Verb RunAs
#Deletes leftover installer files
Remove-Item C:\Users\Ross\Desktop\ccsetup502.exe发布于 2015-02-06 05:00:38
经过一些坚持不懈的努力我终于解决了这个问题。
可能值得注意的是,出于测试目的,我将PSDrive删除留在了那里,因为如果脚本没有完成,并且您在进行更改后尝试再次运行它,您将得到一个错误。
#Ensure previous PSDrive 'p' is removed
Remove-PSDrive P
#Creates new PSDrive
New-PSDrive -Name P -PSProvider FileSystem -Root \\YOURSERVERNAMEHERE\YOURFILEPATHHERE
#Login to server
new-object -typename System.Management.Automation.PSCredential -argumentlist "YOURDOMAINORSERVERUSERNAMEHERE",$password
#Copies installer files from server to the local desktop
Copy-Item -Path \\YOURSERVERNAMEHERE\YOURFILEPATHHERE\ccsetup502.exe -Destination C:\YOURFILEPATHHERE
#Executes copied installers, runs the installer silently, waits until the installer has completed
Start-Process C:\YOURFILEPATHHERE\ccsetup502.exe -ArgumentList "/S" -Wait -Verb RunAs
#Deletes leftover installer files
Remove-Item C:\YOURFILEPATHHERE\ccsetup502.exe希望这能帮助其他被困在未来的人!
感谢任何其他贡献了自己努力的人。
https://stackoverflow.com/questions/28268341
复制相似问题