I'm very new to Windows scripting and am having an issue with trying to execute a Powershell script located on a remote node.
It's a super simple HelloWorld script.
I'm setting up my session and issuing the remote invocation command like this ::
$session = New-PSSession -ComputerName DH2VCAUSPTCTX01.XXX.XXX.com -Credential XXX\XXX
Invoke-Command -Session $session -FilePath C:\Users\Public\EOD_CWx_Scripts\hello_world_PS.ps1
I keep getting this error ::
Invoke-Command : Cannot find path 'C:\Users\Public\EOD_CWx_Scripts\hello_world_PS.ps1' because it does not exist.
At line:1 char:1
+ Invoke-Command -Session $session -FilePath C:\Users\Public\EOD_CWx_Sc ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\Public...lo_world_PS.ps1:String) [Invoke-Command], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand
This is a screen shot of the Remote node showing that the file does indeed exist ::
This is a screen shot of me attempting to invoke the Powershell script on the remote node ::
Like I said, I'm really new to Windows scripting.
Is there something that I'm missing when it comes to remotely invoking Powershell scripts?
For reference, I've been using this resource to try and figure out how to do this :: https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/invoke-command
Related
I have the main.ps1 file where I am importing another file $dirpath\new.ps1 as below:
Import-module $dirpath\new.ps1 -force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
try {
$getval = invoke-command -cn $host -Credential $cred -ScriptBlock {
param($name)
get-myfunc $name
} -ArgumentList $name
} catch {
...
}
Both main.ps1 file and new.ps1 are existing under the same directory - $dirpath.
The new.ps1 looks as below:
Function global:get-myfunc{
PARAM(
[Parameter(Mandatory=$true,Position=0)][STRING]$name
)
write-host "$name"
}
Now, the main.ps1 file is throwing below error:
+ $getval = invoke-command -cn $host -Credential $cred -S ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (get-myfunc:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I have tried renaming new.ps1 as new.psm1 then importing module as Import-module $dirpath\new.psm1 but it is still failing with same error.
I am using poweshell 5
First issue
The new.ps1 file isn't a module, just a regular script, so you can't/shouldn't use Import-Module for that.
Modules are not simply scripts with a different extension. For more information, see:
about Modules
How to Write a PowerShell Script Module
If you want to keep the current setup, dot source the script by replacing the Import-Module line with:
. $dirpath\new.ps1
This will execute the script and make define the function in the current scope.
If the script is located in the same folder as the calling script, you can further simplify the statement by using $PSScriptRoot:
. $PSScriptRoot\new.ps1
Second issue
Invoke-Command means you're running a scriptblock on a different host, which implies that it uses a different scope. That's why it can't find the get-myfunc function.
The modifying your statement to:
Invoke-Command
-ComputerName $host `
-Credential $cred `
-ScriptBlock ${Function:get-myfunc} `
-ArgumentList $name
For a more detailed explanation, see Run Local Functions Remotely in PowerShell.
That Won't work, because you are importing that Module into your local machine. But, the below command is executing in remote machine. That means you need install that module in remote machine and then you can use that module in the remote machine.
$getval = invoke-command -cn $host -Credential $cred -ScriptBlock {
param($name)
get-myfunc $name
Please follow below link for the solution
Good morning guys,
I am quite new with programming in PowerShell..
I have a problem since this morning and I don't understand why.. I am working on a script for my company, and it works perfectly. But the problem is executing it. The script ist named Script.ps1 and I run it through another one who run another PS window with admin proviledges.
$path = 'Script.ps1'
Start-Process powershell.exe -verb RunAs -ArgumentList "-file $path"
Since Friday was working perfectly, but this morning this command gives the following error.
Start-Process : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:1
+ Start-Process powershell.exe -verb RunAs -ArgumentList "-file $path" -NoNewWindo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.StartProcessCommand
(In the error is written also a -NoNewWindow, I used it for understanding which error came out, or the new process disappears in a second)
I tried all.. Do someone can understand what's wrong?
Thanks all!
I am attempting to run an executable through PowerShell 1.0 using another user's credentials. My current script is the following:
$username = "adminuser"
$password = "thepassword"
$secstr = ConvertTo-SecureString -String $password -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $secstr
Start-Process powershell.exe -verb runas -Credential $cred -File C:\Users\Public\myexecutable.exe
The error I receive is the following:
Error: Start-Process : This command cannot be run due to the error: The handle is invalid.
At C:\Users\Public\privy2.ps1:8 char:1
+ Start-Process powershell.exe -Credential $cred
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
When using the command without credentials, the script works and the executable runs:
Start-Process powershell.exe -verb runas -File C:\Users\Public\myexecutable.exe
Any ideas as to why I am getting the The handle is invalid error?
Per .NET Process Start Process Error using credentials (The handle is invalid), I have tried adding redirects but the same error persists.
You need to open Powershell as an administrtor. Simply right-click Powershell and click Run as an Administrtor. It should work just fine and you won't see The handle is invalid error.
I'm trying to download registry hives from computers in my domain, and I'm testing some commands locally first. I have two machines (VMs) not on the same domain: SRV2K12 and US-TEST. I can run reg.exe save HKLM\SYSTEM \\SRV2K12\Hives\<hostname> (where is the local hostname manually typed) successfully on both machines. However, I ultimately need to run the commands remotely, so I'm using Invoke-Command. On my Win 2012 server, I can use Invoke-Command -computername SRV2K12 -ScriptBlock {reg.exe save HKLM\SYSTEM \\SRV2K12\Hives\<hostname> and it works as expected. On my Windows 7 and Windows 10 machines, it doesn't work. I get this error:
ERROR: Access is denied.
+ CategoryInfo : NotSpecified: (ERROR: Access id denied.: String) []. RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : <hostname>
NotSpecified: (:) [], RemoteException
I can run Invoke-Command -ScriptBlock {reg.exe save HKLM\SYSTEM \\SRV2K12\Hives\US-TEST} on the US-TEST Windows 7 machine just fine, and it saves the hive to the remote computer. But when I add the -computername US-TEST attribute (on the Win 7 or 10 machine) it tells me access is denied.
Any ideas how I can get that to work? I have already ran enabled-psremoting -force on those machines and it completes successfully. I also ran Set-item wsman:localhost\client\trustedhosts -value * on both machines. No change.
2-Hop remoting is your problem, as BenH said CredSSP can resolve it but so can a small design change on your part, rather than trying to do the save from the remote host just return the data to your calling computer and do the save from there. Below is one way to do it, but you can get even more efficient and avoid the temp file if you use get-itemproperty instead of reg.exe Also please note that there is a good chance that this will not work when you use invoke-command with the -computername parameter targeting the computer you are running from, -computername needs to be called against a remote machine.
$SB = {
reg.exe save HKLM\SYSTEM "$($env:TMP)\reg"
$tempFile = gc "$($env:TMP)\reg"
return $tempFile
}
$result = Invoke-Command -ScriptBlock $SB -computername COMPUTERNAME
$result | Out-File \\SRV2K12\Hives\US-TEST
I'm trying to run the following code in a function in my script:
$result = Mount-DiskImage -ImagePath $imagepath -PassThru
$driveLetter = ($result | Get-Volume).DriveLetter
Set-Location "$($driveLetter):"
But it constantly fails with this error:
Set-Location : Cannot find drive. A drive with the name 'G' does not exist.
At C:\Users\Agent\BuildAgent\scripts\helpers.psm1:35 char:3
+ Set-Location "$($driveLetter):"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (G:String) [Set-Location], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
But after the script has terminated I can change the drive, no problem.
It might be timing related, but injecting a sleep (even a large one) before setting location, does not help.
Does anyone know about this issue?
Let's all up-vote the bug report.
Then, I think I have a workaround by manually adding the drive as a new PSDrive. I guess the New-PSDrive cmdlet can access the mounted drive even though others can't.
$mount = Mount-DiskImage $isoPath -PassThru
$driveLetter = ($mount | Get-Volume).DriveLetter
# Have to use New-PSDrive so other cmdlets in this session can see the new drive
New-PSDrive -Name $driveLetter -PSProvider FileSystem -Root "$($driveLetter):\"
// ...do things...
Dismount-DiskImage $mount.ImagePath
I can't try this but your command looks odd to me
Set-Location "$($driveLetter):"
have you tried
Set-Location "$driveLetter:"
without the parens