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.
Related
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.
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
I am writing a script where a part of it needs to connect to domain controller and get all the gpo's currently linked to a specific OU.
the line that does that is:
Invoke-Command -Session $S -ScriptBlock {Get-GPInheritance -target $using:Switch -domain shahar.local -server dc01 }
$s= credentials, $switch is a a variable that contains an ou that was picked.
those variables exists and they are good.
the error i get is:
Value does not fall within the expected range.
+ CategoryInfo : NotSpecified: (:) [Get-GPInheritance], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.GroupPolicy.Commands.GetGPInheritanceCommand
+ PSComputerName : DC01
can anyone please assist?
I'm posting this as answer, because IMO there are too many questions that remain 'unanswered' while the solution is given in a comment.
The error message triggered me to check if any of the variables you use perhaps is an Automatic variable and in this case it is the variable $switch.
Using a different self-defined variable name here like $selectedOU should solve the problem.
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($_)
}
I have been tasked with creating a number of active directory groups using PowerShell. The problem is all the group names contain double colons "::" (the naming standard was created long before I joined!).
When running the following command:
NEW-ADGroup –name "XX123::Test_Group_Creation" –groupscope Global –path "OU=TestOU,DC=TestDomain,DC=local"
The following error is received:
NEW-ADGroup : The name provided is not a properly formed account name
At line:1 char:1
+ NEW-ADGroup –name "XX123::Test_Group_Creation" –groupscope Global –path "OU=Test ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (CN=XX123::Test_...Domain,DC=local:String) [New-ADGroup], ADException
+ FullyQualifiedErrorId : The server is unwilling to process the request,Microsoft.ActiveDirectory.Management.Commands.NewADGroup
If you create the group manually (in the gui) you get the following warning but you are still able to create it:
How can I get around this issue. F.Y.I. I have tried to escape the colons with a back tick and also a forward slash, none of which have worked
The : character is not valid for the sAMAccountName attribute, so you need to replace : with another character (the GUI uses _) when creating the group. You can use the -replace operator to do this pretty easily in PowerShell.