- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How scriptblock works in PowerShell?
Scriptblock is a set of commands which can be executed together when they are invoked. In PowerShell, generally, we write individual commands. Scriptblock can be written with two curly brackets.
Example
$sb = {Get-Process powershell; Get-Service W32Time}
Here we have written two commands under scriptblock. If you directly run this command, scriptblock treats them as a string.
PS C:\> $sb Get-Process powershell; Get-Service W32Time
To run the commands inside the scritblock, use Invoke-Command with the -Scriptblock parameter.
Invoke-Command -ScriptBlock $sb
Output
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 529 44 240240 256164 36.02 7668 1 powershell Status : Running Name : W32Time DisplayName : Windows Time
To run the commands on the remote computers, use -ComputerName parameter.
Example
Invoke-Command -ComputerName Test1-Win2k16,Test1-Win2k12 -ScriptBlock $sb
Output
PS C:\Scripts\AppdInstallation_Remote> Invoke-Command -ComputerName Test1- Win2k16,Test1-Win2k12 -ScriptBlock $sb Handles NPM(K) PM(K) WS(K) CPU(s) Id SI P rocessName PSComputerName ------- - ----- ----- ----- ------ -- -- ----------- -------------- 434 29 46200 47564 0.70 3228 1 powershell Test1-Win2k12 Status : Running Name : Spooler DisplayName : Print Spooler PSComputerName : Test1-Win2k12 583 29 57892 64636 0.59 4524 1 powershell Test1-Win2k16 Status : Stopped Name : Spooler DisplayName : Print Spooler PSComputerName : Test1-Win2k16
You can also pass the parameters inside the scriptblock using -ArgumentList parameter in InvokeCommand cmdlet. Declaration of param will be the same as the function in PowerShell.
Example
Invoke-Command -ComputerName Test1-Win2k16,Test1-Win2k12 -ScriptBlock $sb $sb = { param($process,$service) Get-Process -Name $process Get-Service -Name $service } Invoke-Command -ScriptBlock $sb -ArgumentList "PowerShell","W32Time"
Output
NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName ------ ----- ----- ------ -- -- ----------- 43 234.39 22.84 43.61 7668 1 powershell Status : Running Name : W32Time DisplayName : Windows Time
- Related Articles
- How Ternary operator in PowerShell Works?
- How out-gridview selection works in PowerShell?
- How Parallel and Sequence execution works in PowerShell Workflow?
- How to work with Invoke-Command Scriptblock output?
- How "getElementByID" works in JavaScript?
- How JVM works?
- How Web Works?
- How EXPORT_SET() function works in MySQL?
- How Optimization in Machine Learning Works?
- How optional chaining works in TypeScript?
- In MySQL, how IN() comparison function works?
- How TypeScript Compilation Works?
- How the Table statement works in ABAP
- How regular expression grouping works in Python?
- How variable scope works in Python function?

Advertisements