how do I remove-Printer that contains wildcard characters through Powershell? - powershell

I am attempting to remove printers with the Remove-printer command, but keep running into a WildcardPatternException error. The printer name contains [].
This is the error that I am getting when running the command.
Exception calling "FilterByProperty" with "4" argument(s): "The specified wildcard character pattern is not valid:
[Follow_Me_Colour_Duplex [ServerName](Mobility)]"
At line:1153 char:9
+ $__cmdletization_queryBuilder.FilterByProperty('Name', $__cmd ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : WildcardPatternException
Despite the server name being in the printer name, it is a local printer.
Is there a way to ignore the wildcard characters in this string? Or is there another way to get the location / name without the characters being in there?
Thanks in advance.

Related

PowerShell and M2MQTT

I'm trying to create a simple MQTT client in PowerShell to receive some messages from a broker. This broker is using a non standard port.
To do that, I followed the instructions I found at https://jackgruber.github.io/2019-06-05-ps-mqtt/
When using the example's code it's working fine but when I'm trying to specify a port like
$MqttClient = [uPLibrary.Networking.M2Mqtt.MqttClient]("mqtt-broker.net",41383)
it throws the error:
Cannot convert the "System.Object[]" value of type "System.Object[]" to type "uPLibrary.Networking.M2Mqtt.MqttClient".
At line:1 char:1
+ $MqttClient = [uPLibrary.Networking.M2Mqtt.MqttClient]("ext.mqtt.drya ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : ConvertToFinalInvalidCastException
Any ideas?
I suspect you'd need to construct the object, not just cast it.
Try:
[upLibrary.Networking.m2mqtt.mqttclient]::new("mqtt-broker.net",41383)

In Powershell, how do I delete a file with a hyphen (dash) in the filename?

In Powershell, when I try to delete a file with a hyphen in the nam like this:
remove-item 'C:\S3\op_netadmin-47.bak'
I get this error:
remove-item : Cannot remove item C:\S3\op_netadmin-47.bak: Access to the path 'C:\S3\op_netadmin-47.bak' is denied.
At line:1 char:1
+ remove-item 'C:\S3\op_netadmin-47.bak'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\S3\op_netadmin-47.bak:FileInfo) [Remove-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
But if I rename the file to op_netadmin_47.bak, it works fine.
I've tried with and without double quotes as well as single quotes
I tried your file name with - and _, both work ok. I also tried to use " around file name which also works.
As #mklement0 mentioned in comments read-only files require -Force parameter
Make sure user running script has read/write access to given folder

Why do proxy commands handle errors differently

For a while, I am maintaining a PowerShell Join-Object cmdlet.
In here I am creating a few proxy commands with default parameters, as FullJoin-Object, Merge-Object and Insert-Object as described here: Proxy Functions: Spice Up Your PowerShell Core Cmdlets.
(In earlier version I was using aliases which could problems if the user creates its own aliases.)
Everything works as expected except that the error handling differs a little between the main command and the proxy command...
Taken the following MVCE, based on the following function:
Function Inverse([Int]$Number) {
Rubbish
Write-Output (1 / $Number)
}
(Where the function Rubbish doesn't exist)
Than I create a proxy function called Reverse0:
$MetaData = [System.Management.Automation.CommandMetadata](Get-Command Inverse)
$Value = [System.Management.Automation.ProxyCommand]::Create($MetaData)
$Null = New-Item -Path Function:\ -Name "Script:Inverse0" -Value $Value -Force
$PSDefaultParameterValues['Inverse0:Number'] = 0 # (Not really required for reproducing the issue)
If I run the original function Reverse 0, I get two errors:
Rubbish : The term 'Rubbish' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:2 char:1
+ Rubbish
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (Rubbish:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Attempted to divide by zero.
At line:3 char:1
+ Write-Output (1 / $Number)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
If run the proxy command Reverse0 (or Reverse0 0), I get only the first error:
Rubbish : The term 'Rubbish' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:2 char:1
+ Rubbish
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (Rubbish:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : CommandNotFoundException
It seems that something like the $ErrorActionPreference has changed but I checked that and it appears to be the same within both functions.
Is there and explanation for the different behavior?
Is there a way to get a Proxy Command act the same as the original command with respect to error handling?
The code that [System.Management.Automation.ProxyCommand]::Create() generates is currently (PowerShell Core 7.0.0-preview.5) flawed:
It incorrectly propagates statement-terminating errors as script-terminating errors, by using throw rather than $PSCmdlet.ThrowTerminatingError($_) in its catch blocks, causing the function to abort instantly.
If you manually correct these calls, your proxy function should behave as expected.
See this GitHub issue; the linked issue isn't specifically about this behavior, but a change has been green-lighted, and it should include a fix for it.
In concrete terms, for now, you'll have to fix the generated code manually:
Locate all try / catch statements that look like this:
try {
# ...
} catch {
throw
}
and replace them with:
try {
# ...
} catch {
$PSCmdlet.ThrowTerminatingError($_)
}

How to use a variable in Get-MailboxFolderPermission

I wanted to use a variable for a lawyer's name so I could recycle the same Get-MailboxFolderPermission command, but the syntax doesn't seem to work.
The command by itself would be:
Get-MailboxFolderPermission jdoe:\calendar
But if I try to put "jdoe" in a variable ($lawyer = "jdoe")
and then try to invoke it in the command
Get-MailboxFolderPermission $lawyer:\calendar
it gives an error:
Variable reference is not valid. ':' was not followed by a valid variable name character. Consider using ${} to
delimit the name.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : InvalidVariableReferenceWithDrive
Get-MailboxFolderPermission "$($jdoe):\calendar" doesn't give an error, but it also doesn't produce an accurate output of his calendar permission.
I suspect it has to do with the ":" that is part of the command, but I can't find an article that addresses this particular issue.
Sorry, I just figured it out based on the error.
Get-CalendarPermission ${jdoe}:\calendar works properly.

start-job loop how to call second script

Can anyone please suggest me what is wrong - I am calling second script from first so that
I can run the compare in background or parallel, due to bug in IDM software I need to
execute loop two times.
I need to call 5 scripts from my main script ( frist script) so that all five scripts run
parallelly.
First Script -
==================================================
Error message
Attribute cannot be added because it would cause the variable sbtFile with value C to become invalid.
+ CategoryInfo : MetadataError: (:) [Start-Job], ValidationMetadataException
+ FullyQualifiedErrorId : ValidateSetFailure,Microsoft.PowerShell.Commands.StartJobCommand
The command cannot find the job because the CompareCtrlMasterCtrlModelESS name was not found. Verify the value of the Name parameter, and then try the comman
d again.
+ CategoryInfo : ObjectNotFound: (CompareCtrlMasterCtrlModelESS:String) [Wait-Job], PSArgumentException
+ FullyQualifiedErrorId : JobWithSpecifiedNameNotFound,Microsoft.PowerShell.Commands.WaitJobCommand
The command cannot find the job because the CompareCtrlMasterCtrlModelESS name was not found. Verify the value of the Name parameter, and then try the comman
d again.
+ CategoryInfo : ObjectNotFound: (CompareCtrlMasterCtrlModelESS:String) [Receive-Job], PSArgumentException
+ FullyQualifiedErrorId : JobWithSpecifiedNameNotFound,Microsoft.PowerShell.Commands.ReceiveJobCommand
Regards
Naveen
You run Start-Job -Name "CompareCtrlMasterCtrlModelESS" in a loop, so you try to create multiple jobs with the same name. Try Start-Job -Name "CompareCtrlMasterCtrlModelESS$i" (with ordinal suffix).