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
Related
so i just start learn about powershell script
my objective is to uncheck this one
system properties
so i create powershell script to run the file.reg
this is my test1.ps1
$username = "desktop-2ussd\viola"
$password = "qwerty"
$AdminCred = New-Object System.Management.Automation.PSCredential -ArgumentList #($username,
(ConvertTo-SecureString -String $password -AsPlainText -Force))
$regFile = ".\file.reg"
$regArg1 = "import $regFile"
Start-Process reg.exe -ArgumentList $regArg1 -Credential $AdminCred
and this is my file.reg
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server]
"fDenyTSConnections"=dword:00000000
"updateRDStatus"=dword:00000001
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp]
"UserAuthentication"=dword:00000000
after that i run the script like this
powershell -ExecutionPolicy Bypass -File .\test1.ps1
there is no error output but the checkbox is still checked
please help me
Currently you wont recognize if something goes wrong as you do not get a return code. In case of start-process you would need to specify the parameters:
-wait
-passthru
to get the return code.
But you can directly write to the registry from PowerShell instead of using reg.exe. - e.g.:
set-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server" -name fDenyTSConnections -value 0
The above mentioned registry change gets effective immediately without restarting the related service.
Based on your comment you missed to specify the computer where the command should run. Also make use of $using to access variables of the caller machine from the remote machine e.g.:
$code = {
$newValue = $using:value
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" -Name "UserAuthentication" -Value $newValue
}
$value = 0
invoke-command -computername [TargetComputerName] -credential $cred -scriptblock $code
In your example you did pass the value to the paramter -value as $args[0] - this only works if you specify the paramter -argumentlist of the invoke-command cmdlet. But I would advise to use $using as outlined in my example.
I'm trying to get a script together to remotely install some windows updates on some remote servers that are connected in an offline domain.
I have tried regular PS Remoting and after some research, I think what I am trying to do isnt supported by microsoft. When checking my event logs I have a bunch of these errors.
Edit
I wanted to add that I have tried running the .\Install2012R2.ps1 script from my local computer, modified to have the Invoke-Command in that and have it run the update portion of the original Install2012R2.ps1 and I would get the same errors.
I was hoping that by placing the script on each server that it would like that more.
End Edit
Windows update could not be installed because of error 2147942405 "Access is denied."
(Command line: ""C:\Windows\System32\wusa.exe" "C:\Updates\windows8.1-kb4556853-x64.msu" /quiet /norestart")
I have tried running Invoke-Command as credentialed to an administrator account on the servers but I have been having no luck and was looking for some advice if someone has maybe tried/done this before.
$Servers = #("V101-Test1","V101-Test2")
$Username = 'admin'
$Password = 'Password'#not actual password
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
Get-PSSession | Remove-PSSession
New-PSSession -ComputerName $Servers
foreach($Server in $Servers){
Get-ChildItem -Path C:\Source\Temp -Recurse | Copy-Item -Destination "\\$Server\c$\Updates\" -Force
}
Invoke-Command $Servers -Credential $Cred -ScriptBlock{
& "C:\Updates\Install2012R2.ps1"
}
EDIT 2
Here is the actual install code of the Install2012R2.ps1 script
$updatedir= "./"
$files = Get-ChildItem $updatedir -Recurse
$msus = $files | ? {$_.extension -eq ".msu"}
$exes = $files | ? {$_.extension -eq ".exe"}
foreach ($file in $msus){
$KBCtr++
$fullname = $file.fullname
# Need to wrap in quotes as folder path may contain space
$fullname = "`"" + $fullname + "`""
$KBN = $fullname.split('-')[1]
# Need to wrap in quotes as folder path may contain space
$fullname = "`"" + $fullname + "`""
# Specify the command line parameters for wusa.exe
$parameters = $fullname + " /quiet /norestart"
# Start services and pass in the parameters
$install = [System.Diagnostics.Process]::Start( "wusa",$parameters )
$install.WaitForExit()
}
I'm not sure why wusa.exe is failing here with Access Denied, but here is a PowerShell-native approach you can try. If nothing else, it should give you a clearer indication via the captured error information as to what the underlying issue is:
Add-WindowsPackage -Path C:\Updates\OurHeroicUpdate.msu -Online -PreventPending -NoRestart
-Path is the path to the msu file
-Online tells Add-WindowsPackage to modify the currently "mounted image" (the running version) of Windows (as opposed to an offline disk image you could also apply it to)
-PreventPending prevents installing the msu if there is already a pending change, like needing to reboot for updates.
Add-WindowsPackage is part of the DISM module available under Windows PowerShell, and is the functional equivalent of dism /packagepath:"cabfile", although it can take an msu where dism.exe only allows a cab.
I am trying to use this script to install Python on the remote computer. If I run this file directly on the server. This is the Python_Pip_Proxy_PyWinAuto.ps1 file. It works.
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
Write-Host("Hi")
$installer="C:\temp\python-3.6.2.exe"
& $installer /quiet PrependPath=1 InstallAllUsers=1 TargetDir="C:\Python36"
However if I run the Invoke-Command using the following script to run this remotely on the same server, It print's the Hi message so I know that the file is running but Python doesn't get installed.
# Getting the list of servers from a .txt file to an array #
$SRVListFile = "C:\Scripts\ServerList.txt"
$SRVList = Get-Content $SRVListFile -ErrorAction SilentlyContinue
# Copying the .exe file from a shared location to each server in the array #
# Invoking the .ps1 file which runs natively on each server #
Foreach($computer in $SRVList) {
Get-Service remoteregistry -ComputerName $computer | start-service
Copy-item -Path "E:\Software\python-3.6.2.exe" -Destination \\$computer\c$\temp -Recurse
Copy-item -Path "C:\My Files\Work\Episode 003 - MongoDB Back Up\Python_GUI.py" -Destination \\$computer\c$\temp -Recurse
Invoke-Command -ComputerName $computer -FilePath "C:\My Files\Work\Episode 003 - MongoDB Back Up\Python_Pip_Proxy_PyWinAuto.ps1"
}
What is going wrong. What should I change the code to?
Try using the -scriptblock {Your command here} parameter to execute the command inside the scriptblock parenthesis on the remote computer.
Perhaps you can do it like
$Scriptblock = {
PowerShell -file "C:\My Files\Work\Episode 003 - MongoDB Back Up\Python_Pip_Proxy_PyWinAuto.ps1"
"This is Working" | out-file "C:\Hi.txt"
}
Invoke-Command -ComputerName $computer -Scriptblock $Scriptblock
You might want to remove the Write-Host "Hi" part because that gives the script an interactive nature. If you want to check for execution on remote computer, you can use out-file cmdlet to create a file on the remote computer as an indication.
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
I want to use Powershell in order to call a batch file on remote machines. This batch file has arguments. Here's what I have so far:
$script = "\\fileshare\script.cmd"
$server = $args[0]
$args [string]::join(',',$args[1 .. ($args.count-1)])
Invoke-Command -computername $server {$script + ' ' + $args}
After a bit of searching, I found that the Invoke-Command function runs its scriptblock in a whole new process, so you can't put variables in it (they won't get expanded). That's what the -ArgumentList tag is for. So I tried this instead...
Invoke-Command -computername $server {\\fileshare\script.cmd} -ArgumentList "FirstArgument"
That didn't work either... my batch script tells me it's not being passed any arguments. I can't find anything that explicitly says so, but it looks like the -ArgumentList parameter only works on Powershell scripts (it won't feed them to a batch script).
Any ideas how I can use Invoke-Command to call a batch file with arguments?
When you pass the argument list to the scriptblock, try to "receive them" using a PARAM directive. Like this:
Invoke-Command -computername $server {PARAM($myArg) \\fileshare\script.cmd $myArg} -ArgumentList "FirstArgument"
or you can just use the $args automatic variable:
Invoke-Command -computername $server {\\fileshare\script.cmd $args} -ArgumentList "FirstArgument"
The arguments will be passed as arguments to the scriptblock and not directly to your cmd. You have to do:
Invoke-Command {param($script,$arg1) &$script $arg1 } -computername $server -ArgumentList $script,"FirstArgument"
or
Invoke-Command {&$args[0] $args[1] } -computername $server -ArgumentList $script,"FirstArgument"
PS: I don't know what you are doing with $args [string]::join(',',$args[1 .. ($args.count-1)]), it is a syntax error