One-liner PowerShell script - 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?

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 in doubt with complex commands you can try encoding them in Base64 and using -EncodedCommand. See powershell /? for an example.
Does the line work as intended when you enter it directly in PowerShell?

Related

How can I split the result of powershell select-string command?

I have a command like this:
powershell -command "Select-String -Path 'C:\Program Files\Zabbix Agent 2\zabbix_agent2.conf' -Pattern 'ServerActive'"
Which outputs:
C:\Program Files\Zabbix Agent 2\zabbix_agent2.conf:5:ServerActive=79.240.122.98
I want to get the "79.240.122.98" part. How can I do that? I think that the best approach would be to split the string by the "=" character and then get the second item, but I did not find the way to do it.
From a .bat file? This works for me.
powershell ((select-string serveractive 'c:\program files\zabbix agent 2\zabbix_agent2.conf') -split '=')[1]
79.240.122.98

batch file with inline powershell script that contains variables [duplicate]

I need to run a powershell command via CMD
my command is
(Get-WmiObject -Class win32_pnpEntity -Filter 'Name like "ACPI Thermal Zone"')
[1].GetDeviceProperties().DeviceProperties |
Select-Object -Property keyName, data
and i get successful result:
However, when i try to run the same command with cmd, I getting exception in the query execution.
it looks like something with the quote marks in the command.
Cmd command
powershell -command "(Get-WmiObject -Class win32_pnpEntity -Filter 'Name like "ACPI Thermal
Zone"')"
Cmd error
Please help, what do i miss?
Edit
I tried double quotes and escape char with no luck
You might resolve this at a CMD level by escaping the double quotes with a backslash (\"):
PowerShell -command "Get-WmiObject -Class win32_pnpEntity -Filter 'Name like \"ACPI Thermal Zone\"'"
... or as WMI queries also accepts single quotes for names in filters.
You might also simply avoid the double quotes in the WMI query and resolve this with (escaped) single quotes ('') at a PowerShell level:
PowerShell -command "(Get-WmiObject -Class win32_pnpEntity -Filter 'Name like ''ACPI Thermal Zone''')"

Run powershell command with quote marks via cmd

I need to run a powershell command via CMD
my command is
(Get-WmiObject -Class win32_pnpEntity -Filter 'Name like "ACPI Thermal Zone"')
[1].GetDeviceProperties().DeviceProperties |
Select-Object -Property keyName, data
and i get successful result:
However, when i try to run the same command with cmd, I getting exception in the query execution.
it looks like something with the quote marks in the command.
Cmd command
powershell -command "(Get-WmiObject -Class win32_pnpEntity -Filter 'Name like "ACPI Thermal
Zone"')"
Cmd error
Please help, what do i miss?
Edit
I tried double quotes and escape char with no luck
You might resolve this at a CMD level by escaping the double quotes with a backslash (\"):
PowerShell -command "Get-WmiObject -Class win32_pnpEntity -Filter 'Name like \"ACPI Thermal Zone\"'"
... or as WMI queries also accepts single quotes for names in filters.
You might also simply avoid the double quotes in the WMI query and resolve this with (escaped) single quotes ('') at a PowerShell level:
PowerShell -command "(Get-WmiObject -Class win32_pnpEntity -Filter 'Name like ''ACPI Thermal Zone''')"

Powershell For-Loop throws MissingVariableNameAfterForeach

Why do I get the error "MissingVariableNameAfterForeach" for the following:
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "foreach($file in Get-ChildItem C:){((Get-Date)-$file.LastWriteTime).ToString('dd')}"
The command is supposed to print out the day since today of the last file/folder write in C:\
From the help text:
PS C:\> powershell.exe /?
If the value of Command is a script block, the script block must be enclosed
in braces ({}). You can specify a script block only when running PowerShell.exe
in Windows PowerShell.
Try this:
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command {Get-ChildItem C: | ForEach-Object{($_.LastWriteTime).ToString('dd')}}
Your command will work if you enclose the Get-ChildItem in parentheses.
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command ^
"foreach($file in (Get-ChildItem C:)){((Get-Date)-$file.LastWriteTime).ToString('dd')}"
I may have understood your question, because the existing answers do not seem to provide the information I thought you were looking for.
Whilst these examples don't specifically answer the question posed in your title, they are intended to output what I think you were looking for.
Here therefore is my batch-file attempt:
#"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command^
"Get-ChildItem -Path 'C:\'|Sort-Object -Property LastWriteTime|"^
"Select-Object -Last 1|Format-Table -AutoSize -Property Name,"^
"#{Name='DaysOld';Expression={[Int]$((Get-Date)-$_.LastWriteTime).TotalDays}}"
#Pause
And obviously the cmd version, to be on topic:
"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command "Get-ChildItem -Path 'C:\'|Sort-Object -Property LastWriteTime|Select-Object -Last 1|Format-Table -AutoSize -Property Name,#{Name='DaysOld';Expression={[Int]$((Get-Date)-$_.LastWriteTime).TotalDays}}"
Just in case it is simply my misunderstanding, perhaps this batch-file may work for you:
#"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command^
"Get-ChildItem -Path 'C:\'|Sort-Object -Property LastWriteTime -Descending|"^
"Format-Table -AutoSize -Property Name,"^
"#{Name='DayInMonth';Expression={($_.LastWriteTime).ToString('dd')}},"^
"#{Name='DaysOld';Expression={[Int]$((Get-Date)-$_.LastWriteTime).TotalDays}}"
#Pause
cmd version:
"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command "Get-ChildItem -Path 'C:\'|Sort-Object -Property LastWriteTime -Descending|Format-Table -AutoSize -Property Name,#{Name='DayInMonth';Expression={($_.LastWriteTime).ToString('dd')}},#{Name='DaysOld';Expression={[Int]$((Get-Date)-$_.LastWriteTime).TotalDays}}"
In both cases, you'll note because I'm not running a PowerShell script, there is no need to stipulate an execution policy. Commands should work as if being run directly in the PowerShell window.

Disable NetworkAdapter CMD

How can I run this powershell script in cmd?
$adaptor = Get-WmiObject -Class Win32_NetworkAdapter | Where-Object {$_.Name -like "*Wireless*"}
$adaptor.Disable()
Make sure CMD is elevated. You could use WMIC directly from CMD:
wmic nic where "NetConnectionID like '%wireless%'" call disable
you can pass commands to the powershell executeable via the -Command switch and prefixing the script block with &.
powershell -Command "& { $adaptor = Get-WmiObject -Class Win32_NetworkAdapter | Where-Object {$_.Name -like "*Wireless*"}; $adaptor.Disable() }"
You can read more by running the command powershell -?