Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One-liner PowerShell script

Tags:

powershell

I've created this basic one-liner PowerShell script which executes if I run the ad cmdlet for AD and then the rest of the query. But trying to run them together in line it only seems to load the cmdlet and doesn't execute the rest of the cmd.

powershell.exe -command "&{Import-Module ActiveDirectory; Get-AdGroup -Server DC.Mydomain.com -filter 'name -eq "xxxx"'| set-Adgroup -Replace @{wWWHomePage='10.24.218.194'}}"

Why doesn't it run all together like this?

like image 833
IT Department Avatar asked Apr 30 '12 22:04

IT Department


People also ask

What is a PowerShell one liner?

A PowerShell one-liner is one continuous pipeline and not necessarily a command that's on one physical line. Not all commands that are on one physical line are one-liners. Even though the following command is on multiple physical lines, it's a PowerShell one-liner because it's one continuous pipeline.

What is $_ in PowerShell script?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.

What does $() mean in PowerShell?

Subexpression operator $( ) For a single result, returns a scalar. For multiple results, returns an array. Use this when you want to use an expression within another expression. For example, to embed the results of command in a string expression. PowerShell Copy.


3 Answers

The answer was to escape the double quotes:

powershell.exe -noprofile -command "&Import-Module ActiveDirectory; Get-AdGroup -Server server.mydomain.com -filter 'name -eq *\"xxxx\"*'| set-Adgroup -Replace @{wWWHomePage='10.10.10.10'}"

Basically, I'm running this from SQL to update an ActiveDirectory attribute that isn't accessible with DSADD.

like image 95
IT Department Avatar answered Oct 07 '22 12:10

IT Department


# Start-Run, type:
powershell.exe -noprofile -command "[guid]::newguid()|Set-Clipboard"
like image 44
Yordan Georgiev Avatar answered Oct 07 '22 10:10

Yordan Georgiev


It looks like a quoting issue. Try to replace the surrounding filter quotes with braces:

-filter {name -eq "xxxx"}

To avoid these kind of situations, when you have long list commands to execute, I suggest you put the commands in a script file and pass its path to the -File parameter.

like image 22
Shay Levy Avatar answered Oct 07 '22 12:10

Shay Levy