Disable NetworkAdapter CMD - powershell

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 -?

Related

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''')"

Batch script to taskkill search by "Command Line" arguments

I need a batch script to taskkill by "Command Line" arguments ("Command Line" from Windows Task Manager). To clarify - these processes are dotnet core applications. They are started via:
dotnet MyDotnetCoreApp.dll xxx yyy
If you examine under Task Managers,
Name = dotnet.exe
Image path name = C:\Program Files\dotnet\dotnet.exe
Command line = dotnet MyDotnetCoreApp.dll xxx yyy
I need a batch script to kill these tasks, probably with taskkill
OPTION 1 is Taskkill by PID but how my script search "Command Line" arguments for MyDotnetCoreApp?
OPTION 2 is taskkill by Image Name? This is no go as my server has many dotnet core applications, if kill my Image Name, all dotnet core processes be killed
I been researching:
https://superuser.com/questions/415360/how-do-i-find-out-command-line-arguments-of-a-running-program
https://www.itprotoday.com/powershell/powershell-contains
I can't get this to work, not good at PowerShell:
Get-WmiObject Win32_Process -Filter "name = 'dotnet.exe'" | Select-Object Handle
Here'd get a list of PIDs with which to kill.
Two challenges:
First Challenge, my WHERE clause dont work:
Get-WmiObject Win32_Process -Filter "name = 'dotnet.exe'" | where {$_.CommandLine -like '*MyDotnetCoreApp*'} | Select-Object Handle
I checked further, found out these "CommandLine" was NOT populated for these WmiObjects (omg!):
Get-WmiObject Win32_Process -Filter "name = 'dotnet.exe'" | Select-Object ProcessId, Name, CSName, Caption, CommandLine, ExecutablePath
I later then found out "CommandLine" would have been populated IF you run Powershell as Administrator!?! (Powershell so cryptic!)
In the end - First challenged was resolved:
Get-WmiObject Win32_Process -Filter "name = 'dotnet.exe'" | where {$_.CommandLine -like '*MyDotnetApp*'} | Select-Object ProcessId, Name, CSName, Caption, CommandLine, ExecutablePath
Second Challenge: How to kill it? Found it!!
(Get-WmiObject Win32_Process -Filter "name = 'dotnet.exe'" | where {$_.CommandLine -like '*MyDotnetCoreApp*'}).Terminate()
So this is actually resolved!
Run Powershell as Administrator! Download psexec from https://learn.microsoft.com/en-us/sysinternals/downloads/psexec
psexec -u Administrator -p SomeSecret powershell
Then from Powershell:
(Get-WmiObject Win32_Process -Filter "name = 'dotnet.exe'" | where {$_.CommandLine -like '*MyDotnetCoreApp*'}).Terminate()
Now as separate question, can you do this one line? Below wont work because -Filter has quotes in it!
psexec -u Administrator -p SomeSecret powershell -Command "(Get-WmiObject Win32_Process -Filter ""name = 'dotnet.exe'"" | where {$_.CommandLine -like '*MyDotnetCoreApp*'}).Terminate() "
As hacky work around, I removed -Filter clause (How unfortunate, not sure how to escape quotes):
psexec -u Administrator -p SomeSecret powershell -Command "(Get-WmiObject Win32_Process | where {$_.CommandLine -like '*MyDotnetCoreApp*'}).Terminate() "
Works for me as a regular user, unless the process is running as administrator?. Unfortunately, the filter syntax is like sql here, where '%' is the wildcard. Piping to where-object would probably work just as well.
get-wmiobject win32_process -filter "commandline like '%dotnet.exe%MyDotnetCoreApp%'" |
remove-wmiobject
get-wmiobject win32_process | where commandline -like '*dotnet.exe*MyDotnetCoreApp*' |
remove-wmiobject

Calling Powershell in a batch file issue

My powershell command is this:
Get-WmiObject -class Win32_printer | ft name, systemName, shareName >> $PSScriptRoot\printers.txt
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, InstallDate | Format-Table –AutoSize >> $PSScriptRoot\programs.txt
Get-WmiObject -Class Win32_MappedLogicalDisk | select Name, ProviderName >> $PSScriptRoot\drives.txt
My batch is this:
#ECHO OFF
SET Directory=%~dp0
SET PowerShellScriptPath=%Directory%reimage_checklist.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'";
If I run the PowerShell script in IDE, it puts out three text documents with the requested information. If I run the batch file, it outputs three batch files, but the network drive text file is blank.
Resolved by adding
Set-ExecutionPolicy -Scope CurrentUser Bypass
To the powershell script and removing
-ExecutionPolicy Bypass
From the batch file

Flushdns and registerdns on multiple machine by powershell

Simple question but not able to find answer on google at the moment. My powershell version is 2. I want to flush and registerdns on multiple machines.
ipconfig /flushDns
ipconfig /registerdns
I can't use invoke command and psremoting is not enabled on machines.
Any advise how to flushdns & registerdns.
It's pretty easy with Invoke-wmimethod
Create a list of your computers in a file named servers.txt, then create a script like this :
$listofservers = Get-Content .\servers.txt
foreach ($servers in $listofservers) {
Invoke-WmiMethod -class Win32_process -name Create -ArgumentList ("cmd.exe /c ipconfig /flushdns") -ComputerName $servers
Invoke-WmiMethod -class Win32_process -name Create -ArgumentList ("cmd.exe /c ipconfig /registerdns") -ComputerName $servers
}
By default you'll not get the output of the command, but you'll only get information if the command sucessfully ran on remote computer through this value :
ReturnValue
If this value equal to 0 that means the command was sucessfully executed on the remote server.
If you want to get the command output, you can achieve it but adding output redirection to txt file :
$listofservers = Get-Content .\servers.txt
foreach ($servers in $listofservers) {
Invoke-WmiMethod -class Win32_process -name Create -ArgumentList ("cmd.exe /c ipconfig /flushdns > c:\flushdnsresult.txt") -ComputerName $servers
Invoke-WmiMethod -class Win32_process -name Create -ArgumentList ("cmd.exe /c ipconfig /registerdns > c:\registerdnsresult.txt") -ComputerName $servers
}
Then you'll find a txt file on your remote server containing the result output of cmd command.
If you upgrade your powershell version from 2 (highly recommended - I have a powershell & dotnet update script to do this also) you can use:
# Get Windows servers on Domain
####################
$serversraw=Get-ADComputer -Filter {(OperatingSystem -like "*windows*")}
# Filter responsive
####################
$serversup = $serversraw.name | where {Test-Connection $_ -quiet -count 1}
# Flush DNS & reregister
####################
Clear-DnsClientCache -cimsession $serversup
Register-DnsClientCache -cimsession $serversup