Invoke-command and run commands with administrator account

With the following command, I am trying to run dir c: with the administrator account from a powershell

$RemoteMachine = "10.1.1.40" $Username = 'Administrator' $Password = ' ' $pass = ConvertTo-SecureString -AsPlainText $Password -Force $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass Invoke-Command -ComputerName $RemoteMachine -Credential $Cred -ScriptBlock { dir c: } 

However, the output is:

PS C:\Windows\system32> C:\Users\user\Desktop\anydesk.ps1 PS C:\Windows\system32> 

That means, it has not connected to the remote machine. I have to say that administrator account is enabled and the remote desktop allows administrator. Any idea to fix that? If I use a normal user, the am able to see the output of dir c:.

enter image description here

enter image description here

1 Answer

Is the password for Administrator on the remote machine actually blank?

Try doing each step manually to verify what does and does not work:

# Get-Credential will prompt for a username and password # Try and use the computer *name* instead of the IP: $RemoteMachine = 'MyPC' $cred = Get-Credential -UserName "$RemoteMachine\Administrator" # Use a remote Powershell session instead of invoke-command for troubleshooting: Enter-PSSession $RemoteMachine -Credential $cred # In the remote session, manually run "dir C:/" [MyPC]: PS C:\WINDOWS\system32> dir C:/ 

Then add the results and/or error messages to your question

If the above works, try the following - it's not much different from what you tried earlier:

$computername = 'MyPC' $user = "$computername\Administrator" $password = ConvertTo-SecureString -AsPlainText (Read-Host "MyPassword") -Force $cred = [PSCredential]::new($user, $password) Invoke-Command -ComputerName $computername -Credential $cred -ScriptBlock {Get-ChildItem c:} 

I want to add that leaving your admin password in plaintext in the script is a bad idea. Please consider storing it somewhere more secure, or use something like the CredentialManager powershell module.

5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like