【问题标题】:Powershell PS CredentialsPowershell PS 凭据
【发布时间】:2015-03-31 20:15:47
【问题描述】:

我在尝试使用 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

提前致谢!

罗斯

【问题讨论】:

  • 您遇到了什么错误,在哪一行 - New-PSDrive 行还是 Copy-Item 行?
  • Ross 不是凭证对象。您可以使用-Credential (Get-Credential Ross),它会提示您输入用户名和密码(用户名预先填写为“Ross”)。但了解@KeithHill 要求的信息会很有帮助。

标签: windows shell powershell powershell-3.0


【解决方案1】:

试试这个。它会提示您输入凭据,但您也可以随时创建它们并将它们存储在变量中。

#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

【讨论】:

    【解决方案2】:

    经过一番坚持,我设法解决了这个问题......

    可能值得注意的是,出于测试目的,我将 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
    

    希望这可以帮助其他陷入未来的人!

    感谢其他做出贡献的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-15
      • 2018-07-08
      • 1970-01-01
      • 1970-01-01
      • 2019-04-03
      • 2013-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多