Questions tagged [scriptblock]

Single unit of operation of expression and statements.

Multiple expressions or statements which make up a single unit of operations. The block can accept return values and arguments. Usually found in a statement list in braces such as {<list of statements>}.

154 questions
0
votes
3 answers

Exit not Exiting in invoke-command in remote ps session

I have a Function invokes a command that starts a new ps session on a remote server. The invoke command has an Exit clause however this is not exiting? Function CreateID{ Invoke-Command -Session $Script:sesh -ScriptBlock{ Set-Location c:\ …
LDStewart
  • 117
  • 3
  • 11
0
votes
1 answer

scriptblock returning object for compare-object

I'm writing a script to compare 2 samba locations via compare object. To speed things up i would like to give each location via a thread to a scriptblock where i let the object get made. After that i'd like the output from the scriptblock as an…
Michael
  • 57
  • 1
  • 11
0
votes
0 answers

Runspace scriptblock array cannot be passed

I'm tinkering with the code below. It should create a PoshRSJob and run the function foo again in the runspace there. I want to be able to turn the $list parameter into an [array] or [string[]], but when I do it throws errors. I considered…
CuriousOne
  • 922
  • 1
  • 10
  • 26
0
votes
1 answer

issue with variable in Powershell

Hello I am using below scriptblock and have passed variable local folder where while testing path it is coming $Null. could you please suggest what I am missing here $ScriptBlockDir
Suuny
  • 117
  • 1
  • 1
  • 9
0
votes
2 answers

Calling PowerShell Start-Job cmdlet with multiple parameters and foreach loop is soooo close

I have several thousand computers to backup the Security event logs to a server's share. The environment is very dynamic, hence the need to automate this. I've been working on a script which creates a hash wherein each key is a sequence and the…
GaryG
  • 13
  • 5
0
votes
1 answer

PowerShell Test-Path not working in scriptblock

I have the following PowerShell code that tests if a file exists within a script block: $scriptblock = { Param($filename) return "Scriptblock filename $filename Exists? -> $(Test-Path $filename)" } $myFilename = "MyFile.xml" Write-Host…
Dili
  • 556
  • 1
  • 5
  • 14
0
votes
1 answer

Powershell Passing Parameters to Nested Start-Job

I am attempting to pass paramters to a nested job. $executepath = "D:\nttools\CoreAutomation\$patch.zipfilename\$ntupdatefilename" $sb = {Start-Job -Name $NTUpdate -ScriptBlock {& cmd.exe /c $executepath}} Invoke-Command -Session $NTSession…
BilliAm
  • 590
  • 2
  • 6
  • 26
0
votes
1 answer

Invoke-Command with ScriptBlock Works on Local Server - Remotely Resultset is Empty

When trying to obtain the Citrix XenApp 6.5 server status by using the following code, a resultset is returned when run in PowerShell locally on a Zone Data Collector: $serverName = "SOMECITRIXSERVER" $Invoke-Command -ScriptBlock {Add-PSSnapin…
DStackley
  • 1
  • 1
0
votes
1 answer

Can Invoke-Command be used to call Invoke-Command on a third system?

I've got a segmented Active Directory network where multiple VLAN's can only talk to a local AD server, but all AD servers can talk to each other. I'd like to have a script that can proxy through each AD server to run a command on each system per…
Stanton
  • 65
  • 1
  • 9
0
votes
1 answer

If Else within an Invoke-Command Powershell

I am trying to run the below command to say if this registry key exists then Get-ItemProperty Else Do nothing or Display Text for Testing. "SQL Server Product Name" = Invoke-Command -ComputerName $Computer -ScriptBlock {If (Test-Path…
onefiscus
  • 129
  • 1
  • 3
  • 15
0
votes
2 answers

PowerShell query remote SQL Server with different Windows Credentials

I am trying to query a SQL Server using a a script block and passing in a different set of windows credentials. The script works if i run it on the same machine as the SQL Server but fails when i try and run it remotely. I know i can access the SQL…
0
votes
0 answers

Run batch file in ScriptBlock called from Start-Job command

Trying to run a batch file and sending a parameter from ScriptBlock. And I am calling ScriptBlock from Start-Job command $ScriptBlock = { param($file) E:\test\test.bat $file } Start-Job -ScriptBlock $ScriptBlock -ArgumentList…
asim
  • 31
  • 1
  • 8
0
votes
0 answers

Need help writing better code

Context: This script/function lives on my local machine. I want to pass variables to the function, then pass them into an invoke-command script block to be run on a remote machine. This code works, but I don't feel like its good practice, or the…
0
votes
1 answer

Passing array to one of named parameters in script block?

I am trying to figure out a way to pass an array to a named parameter in a separate script. However, I could not find any solution. Test2.ps1: param( [int]$a, [int]$b, [int]$c, [string[]]$d ) write-host "`$a = $a" write-host "`$b =…
0
votes
1 answer

Creating scriptblocks from string

Here is what I am trying to get to work in a scriptblock $scriptblock={ for /f "tokens=14" %i in ('"ipconfig | findstr IPv4"') do set ip=%i nslookup %ip% } Everytime though I get Exception calling "Create" with "1" argument(s): "At line:4 char:4 …
CRad14
  • 3
  • 1