我正在写一个脚本来检查我们的备份是否成功。不幸的是,备份位于不同的位置/登录。该脚本计划在没有用户干预的情况下运行,就像其他人一样。我添加了保存的凭据信息,但-Credential选项不起作用。它仍然要求我在弹出登录窗口中输入登录信息,即使使用-credential选项我也是如此。您是否看到以下问题?我知道安全登录与另一个脚本一起工作,所以我不认为它的创建存在问题。我认为问题在于使用带有Test-Path的凭据。从我所看到的情况来看,根据这个链接,我认为我正确地做到了,尽管他们并没有像我一样使用存储的凭据:test-path with cred
function GetSecureLogin (){
#this function will obtain secure login
$TISusername = "username/here"
#read-host -assecurestring | convertfrom-securestring | out-file C:\filename.txt
#Note: when you save password for re-use as above, programs that use it later must be using same login as had when saved password: ie. Script_Guru
$TISpassword = get-content C:\Scripts\filename.txt | convertto-securestring #may not need to re-convert to secure string since stored secure string
$global:cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $TISusername, $TISpassword
}
Function CheckBackedUp($file, $destinationFolder)
{
#get filename without folder
$filename = $file.substring($file.lastindexofany("\") +1 ,$file.length - ($file.lastindexofany("\")+1))
#$destinationFolder = split-path($file)
#get date and append to fileNameWithoutExt
#$ext = Get-Date -uformat "%Y-%m-%d"
$ext = "2014-06-11"
$newFileName = $destinationFolder+"\"+$ext+"*"+$filename
#look for file
if(Test-Path "$newFileName" -Credential $global:cred) #this isn't working
{
Write-Host "$newFileName exists"
}
else
{
$temp = "$newFileName doesn't exist. "
Write-Host "$temp "
$global:resultMessage += $temp
}
#return failure message if don't find file with signature that contains $ext
} #CheckBackedUp
####start here
...
$global:cred = ""
GetSecureLogin
$global:resultMessage = ""
#loop through files, checking if we backed each one up
$i=0
try{
for ($i = $fileArray.GetLowerBound(0); $i -le $fileArray.GetUpperBound(0); $i++) {
$tempSource = $fileArray[$i]
#see if files were correctly backed up
CheckBackedUp $tempSource $TisToLocation
}
...
** 2014年6月27日更新** 当我尝试使用以下代替上面的Test-Path if语句时
invoke-command -computername "usa02xxnas1.na.xxx.net" -credential $globalcred -scriptblock{Test-Path "$newFileName"}
我收到了此错误消息,我不明白如何修复:
[usa02xxxnas1.na.xxx.net] Connecting to remote server usa02xxnas1.na.xxx.net failed with the following error message : WinRM cannot process the request. The following error
with errorcode 0x80090311 occurred while using Kerberos authentication: There are currently no logon servers available to service the logon request.
Possible causes are:
-The user name or password specified are invalid.
-Kerberos is used when no authentication method and no user name are specified.
-Kerberos accepts domain user names, but not local user names.
-The Service Principal Name (SPN) for the remote computer name and port does not exist.
-The client and remote computers are in different domains and there is no trust between the two domains.
After checking for the above issues, try the following:
-Check the Event Viewer for events related to authentication.
-Change the authentication method; add the destination computer to the WinRM TrustedHosts configuration setting or use HTTPS transport.
Note that computers in the TrustedHosts list might not be authenticated.
-For more information about WinRM configuration, run the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OpenError: (usa02xxnas1.na.xxx.net:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : AuthenticationFailed,PSSessionStateBroken