1

I want to execute below code in the either local or remote machine whith current user.

$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 }
}

the @argList is

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

I notice that

this one, it works well (no -comupterName and -session)

Invoke-Command -ScriptBlock $BackUpSqlAgentAndRemoveOldbackup -argumentList $argList

this one, it throw execption (I also tried "-session", get same result)

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

the exception is as below, it seems the it can not access the folder.

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

does anyone know how can I do if I want to add computerName or session? (notes:[Environment]::UserName return identical user)

2 Answers 2

2

You have run into the double hop problem. Your credentials can be transferred to the next machine (first hop), but no further (second hop). This means that you can't use the credentials of the machine where you are executing Invoke-Command on, the remote machine (localhost) to connect to a file share (\Host5FileSrv\Backup). Even if you use localhost as computername, it is still remoting. A solution could be CredSSP. See here and here for more information.

double hop

Sign up to request clarification or add additional context in comments.

Comments

1

This looks like a "second hop" remoting problem, and you'll need to configure WinRM on the computers involved to use CredSSP

http://msdn.microsoft.com/en-us/library/windows/desktop/ee309365(v=vs.85).aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.