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?
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.
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.
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.
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.
# Start-Run, type:
powershell.exe -noprofile -command "[guid]::newguid()|Set-Clipboard"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With