Powershell: Command will not work with "powershell.exe -command "command" - powershell

This Code will work:
Get-NetAdapter | Where-Object {$_.InterfaceDescription -match 'Ethernet' -or $_.InterfaceDescription -match 'Wireless'}
I want start it from Taskscheduler (cmd) or in powershell
This code will not work:
Powershell.exe -Command "Get-NetAdapter | Where-Object {$_.InterfaceDescription -match 'Ethernet' -or $_.InterfaceDescription -match 'Wireless'}"
Error:
... etAdapter | Where-Object {.InterfaceDescription -match 'Ethernet'-or ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : ObjectNotFound: (.InterfaceDescription:String) [Where-Object], CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.WhereObjectCommand
Also as an base64 string same error.
Have anyone an idea why it will not work as an command but direct as command in ps?

Powershell.exe -Command "Get-NetAdapter ; Where-Object {$_.InterfaceDescription -match 'Ethernet' -or $_.InterfaceDescription -match 'Wireless'}"

Related

If program exists uninstall

I'm trying to understand the .uninstall() method.
From this link it looks like the method .uninstall() only works when used with Get-WmiObject -Class Win32_Product. But this means it will consider 32-bit software only and not 64-bit software.
So I wrote this few lines in order to uninstall Erlang, which is 64-bit:
# Check if a Software ins installed
function Check_Program_Installed($programName) {
$x86_check = ((Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall") |
Get-ItemProperty |
Where-Object {$_.DisplayName -like "*$programName*" } |
Select-Object -Property DisplayName, UninstallString) |
Format-Table
if (Test-Path 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall') {
$x64_check = ((Get-ChildItem "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall") | Get-ItemProperty | Where-Object {$_.DisplayName -like "*$programName*" } | Select-Object -Property DisplayName, UninstallString) | Format-Table
}
if ($x86_check -and $x64_check -eq $null) {
Write-Host "$programName is not installed on this computer" -ForegroundColor Green
#continue
} elseif ($x86_check -or $x64_check -ne $null) {
Write-Host "On this computer is installed " -ForegroundColor Red
$x86_check
$x64_check
$x86_check.uninstall()
$x64_check.uninstall()
}
}
# Erlang check
Write-Host "Checking if Erlang exist " -NoNewline
Check_Program_Installed("Erlang")
Write-Host "The End: the script ends here" -ForegroundColor Yellow
but unfortunately it returns me the error:
You cannot call a method on a null-valued expression. At
C:\Users\Admin\Desktop\test.ps1:17 char:3
+ $x86_check.uninstall()
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Method invocation failed because
[Microsoft.PowerShell.Commands.Internal. Format.FormatStartData] does
not contain a method named 'Uninstall'. At
C:\Users\Admin\Desktop\test.ps1:18 char:3
+ $x64_check.uninstall()
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
I believe the root cause it that there are DisplayName and UninstallString in the variable, right?
A way out I found is to use:
'"C:\Program Files\erl8.3\Uninstall.exe'" | cmd
in order to uninstall but this is not using the .uninstall() method which is what I want to use.
Is Microsoft saying that you can use .uninstall() only with 32-bit architecture and for 64-bit you need to find your own way out?
If so it's quite rudimentary
The reply is NO.
.uninstall() can only be used with Get-WmiObject -Class Win32_Product and therefore will only uninstall 32-bit programs.
Source 1
Source 2
Source 3
Source 4
Source 5
There might be an alternative way out uninstall both 32-bit and 64-bit program with:
Get-Package "*Erlang*"
At least finds the program but
Get-Package "*Erlang*" | Uninstall-Package -Force
won't uninstall

Unable to run a query to return information based on 3 different conditions on exchange shell

Somewhat new to exchange shell. I'm wanting to run a query to return exchange resource/equipment mailboxes that matches certain conditions
Get-mailbox -RecipientTypeDetails RoomMailbox, EquipmentMailbox | foreach-object {Get-CalendarProcessing $_.alias | select identity, AllowConflicts, ConflictPercentageAllowed, MaximumConflictInstances | where {($_.MaximumConflictInstances >=1) -and ($_.AllowConflicts -eq $true) -and ($_.ConflictPercentageAllowed >=1)}} | export-csv h:\test12346.csv
But I'm getting this below error
out-file : Access to the path 'C:\Windows\System32\=1' is denied.
At line:1 char:212
+ ... nces | where {($_.MaximumConflictInstances >=1) -and ($_.AllowConflicts -eq $tru ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Out-File], UnauthorizedAccessException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand
I know potentially or my condition syntaxes are incorrect for the MaximumConflictInstances and or AllowConflicts paramters because when I ran the below command (partially of initial command), it works fine as expected
Get-mailbox -RecipientTypeDetails RoomMailbox, EquipmentMailbox | foreach-object {Get-CalendarProcessing $_.alias | select ide
ntity, AllowConflicts, ConflictPercentageAllowed, MaximumConflictInstances | where {($_.AllowConflicts -eq $true)}} | export-csv h:\allowC.csv
I've tried the below and now appears I'm missing something. Is someone able to help me review my code and advise what I'm missing?
Get-mailbox -RecipientTypeDetails RoomMailbox, EquipmentMailbox | foreach-object {Get-CalendarProcessing $_.alias | select identity, AllowConflicts, ConflictPercentageAllowed, MaximumConflictInstances | where {($_.AllowConflicts -eq $true) -and {($_.MaximumConflictInstances -gt 1) -OR ($_.ConflictPercentageAllowed -gt 1)}} | export-csv h:\test12346.csv
Long story short, im just trying to export all exchange room objects and equipment objects if their paramters - allowconflicts is set to TRUE, and MaximumConflictInstances & ConflictPercentageAllowed is equals to OR greater than 1 for both
Thanks
Rob
When doing a comparison, PowerShell does not use the ">=" comparison operator. It should look like this:
where-object { ($_.MaximumConflictInstances -ge 1) -and ($_.AllowConflicts -eq $true) -and ($_.ConflictPercentageAllowed -ge 1)}
You can review the PowerShell comparison operators here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-6

PowerShell Script "A positional parameter cannot be found that accepts argument"?

I am currently trying to run a script that is as follows (to find and uninstall CCleaner):
Get-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object {$_.DisplayName -eq "CCleaner"} -OutVariable Results **{
& "$($Results.InstallLocation)\uninst.exe"
}
The error is:
Where-Object : A positional parameter cannot be found that accepts argument
& "$($Results.InstallLocation)\uninst.exe" /S
.At line:1 char:98
+ Get-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object <<<< {$_.DisplayName -eq "CCleaner"} -OutVariable Results {
+ CategoryInfo : InvalidArgument: (:) [Where-Object], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.WhereObjectCommand
The last part there seems to be a problem. I am sure this is because I am writing this in PS v3 but I'm running this as a PSSession on PC's running PS v2 or v1.
I think this is what you're after:
Get-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object {$_.DisplayName -eq "CCleaner"} | ForEach-Object { & "$($_.InstallLocation)\uninst.exe" }
Otherwise, you need to use a named parameter after "Results"

How To Ignore Error In Powershell Script (ErrorAction: SilentlyContinue not suppressing error)

I have a Powershell script that lists all the users/groups in the local administrators group for all computers in a designated OU in Active Directory.
The script works perfectly locally (if I run it against the local machine only) but when I run it against remote machines, it technically works but it throws a consistent error that I don't know how to filter out.
Here is the script (NOTE: must be running from ActiveDirectory PS console to use Get-ADComputer):
Get-ADComputer -SearchBase 'OU=ou01,dc=domain,dc=local' -Filter 'ObjectClass -eq "Computer"' `
| ForEach-Object {
Get-WmiObject win32_groupuser -cn $_.name -ErrorAction SilentlyContinue `
| Where-Object { $_.groupcomponent -match 'administrators' } `
| ForEach-Object -ErrorAction SilentlyContinue {[wmi]$_.partcomponent } `
| Select-Object __SERVER,Caption
} | Format-Table -Property * -AutoSize
Here are the results (correct result is in first line, error below that):
__SERVER Caption
-------- -------
workstation_name workstation_name\Administrator
Cannot convert value "\\workstation_name\root\cimv2:Win32_Group.Domain="DOMAIN",Name="Domain Admins"" to type "System.Management.ManagementObject". Error: "Not found "
At line:1 char:306
+ Get-ADComputer -SearchBase 'OU=ou01,dc=domain,dc=local' -Filter 'ObjectClass -eq "Computer"' | ForEach-Object { Get-WmiObject win32_groupuser -cn $_.name -ErrorAction SilentlyContinue | Where-Object { $_.groupcomponent -match 'administrators' } | ForEach-Object -ErrorAction SilentlyContinue {[wmi]$_. <<<< partcomponent } | Select-Object __SERVER,Caption } | Format-Table -Property * -AutoSize
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
I have unsuccessfully tried to use -ErrorAction SilentlyContinue, is there another way to suppress this message? Not sure what I am missing.

Simple Example: Expressions are only allowed as the first element of a pipeline

Can explain why philosophically this doesn't work?
Just as a learning example, I wanted to see the properties of the get-service cmdlet, without the events or methods.
PS C:\Users\Neal> get-service | get-member | {$_.name -eq "Property"}
Result:
At line:1 char:29
+ get-service | get-member | {$_.name -eq "Property"}
+ ~~~~~~~~~~~~~~~~~~~~~~~~
Expressions are only allowed as the first element of a pipeline.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline
{$_.name -eq "Property"} is just a scriptblock. If you want to use Where-Object to filter the results of get-member, you need to type Where-Object:
PS C:\Users\Neal> get-service | get-member | Where-Object {$_.name -eq "Property"}
or you can use where, which is an alias for Where-Object:
PS C:\Users\Neal> get-service | get-member | where {$_.name -eq "Property"}
There is even a special character ? which refers to Where-Object:
PS C:\Users\Neal> get-service | get-member | ? {$_.name -eq "Property"}
All three examples given above do the same thing. Choosing between them is simply a matter of style.