2013-03-30 55 views
1

我想在當前用戶的本地或遠程機器中執行下面的代碼。如果我使用powershell invoke-command不起作用--computerName

$BackUpSqlAgentAndRemoveOldbackup = { 
    param([string]$AppServer,[string]$SqlInstance,[string]$BackupShare,[string]$alias) 

    [Environment]::UserName #I got same user name in all cases. 

    [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Smo') | Out-Null 

    $server = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $SqlInstance 

    $backupName = 'SqlAgentJob_' + $SqlInstance + '_' + (Get-Date –format ‘yyyyMMdd_HHmm’) + '_' + $alias + '.sql' 
    $backupPath = join-path $BackupShare $backupName 

    $oldBackups = Get-ChildItem $backupShare | where { ($_.name -like 'SqlAgentJob_*.sql') } 
    $server.JobServer.Jobs.Script() | Out-File -filepath $backupPath 
    foreach ($item in $oldBackups) { remove-item $item.fullName } 
} 

的@argList是

@('hafcapp-1', 'hafcsql-1', '\\Host5FileSrv\Backup\test','auto') 

我注意到

這一塊,它工作得很好(無-comupterName和-session)

Invoke-Command -ScriptBlock $BackUpSqlAgentAndRemoveOldbackup -argumentList $argList 

這一塊,它拋出執行(我也試過「 - 激情」,得到相同的結果)

Invoke-Command -computerName localhost -ScriptBlock $BackUpSqlAgentAndRemoveOldbackup -argumentList $argList 

異常情況如下,看來它不能訪問該文件夾。

Cannot find path '\\Host5FileSrv\Backup\test' because it does not exist. 
    + CategoryInfo   : ObjectNotFound: (\\Host5FileSrv\Backup\test:String) [Get-ChildItem], ItemNotFoundException 
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand 

You cannot call a method on a null-valued expression. 
    + CategoryInfo   : InvalidOperation: (Script:String) [], RuntimeException 
    + FullyQualifiedErrorId : InvokeMethodOnNull 

Cannot bind argument to parameter 'Path' because it is null. 
    + CategoryInfo   : InvalidData: (:) [Remove-Item], ParameterBindingValidationException 
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RemoveItemCommand 

沒有人知道如果我想添加computerName或session,該怎麼辦? (注:[環境] ::用戶名返回相同的用戶)

回答

2

您已遇到雙跳問題。您的憑證可以轉移到下一臺機器(第一跳),但不能再繼續(第二跳)。這意味着您不能使用正在執行Invoke-Command的計算機的憑據,遠程計算機(本地主機)連接到文件共享(\ Host5FileSrv \ Backup)。即使你使用localhost作爲計算機名,它仍然是遠程處理。解決方案可以是CredSSP。有關更多信息,請參閱herehere

double hop

相關問題