Get-credential and cim session - easy example

Good day,

a while ago, I found out that this does not work anymore.

$credentials = New-PSSession -ComputerName $server -Credential (Get-Credential domain\user);

Copy-Item -Path $source -Destination $dest1 -Credential $credentials
Invoke-Command -ComputerName $server -Credential $credentials -ScriptBlock { Msiexec /i C:\testmsi.msi /log C:\testlog.log }

The goal is to execute 2 commands on a remote computer by entering the credentials only once. I searched for examples multiple times already, read Microsoft’s site about cim session but did not yet manage to realize this. Also, I read a thread about it in this forum, did not help.

Does anyone have a simple example that does exactly what I want? I would like to enter the user/pwd only once and execute with these credentials multiple commands on a remote server. Something pretty basic.

Best Regards

Why do you use New-PSSession to create a credentials object? :thinking: :man_shrugging:t3:

$credentials = Get-Credential domain\user

Copy-Item -Path $source -Destination $dest1 -Credential $credentials
Invoke-Command -ComputerName $server -Credential $credentials -ScriptBlock { Msiexec /i C:\testmsi.msi /log C:\testlog.log }

Because it gives me the following error when I use it the way you proposed it:

Copy-Item : Cannot retrieve the dynamic parameters for the cmdlet. The FileSystem provider supports credentials only
on the New-PSDrive cmdlet. Perform the operation again without specifying credentials.

So you should use the way the help recommends …

… use Invoke-Command to impersonate another user, or elevate your credentials when running this cmdlet. :man_shrugging:t3:

thanks that did the trick.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.