Hello,
I have a custom PowerShell configuration (PSSessionConfiguration w/ Startup Script) to allow some folks the ability to run a subset of cmdlets from a remote session. This has been working great and I wanted to extend the functionality to incorporate the Get-GPOReport cmdlet. So as I have in the past, I added this cmdlet to my $allowedCmdlets array in the startup script for this session configuration. When I enter a remote session under this configuration and execute the cmdlet with the -ReportType parameter as "Xml" (Get-GPOReport -ReportType Xml) I get the following error:
Operation is not valid due to the current state of the object.
+ CategoryInfo :
+ FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.GroupPolicy.Commands.GetGpoReportCommand
I can run the same cmdlet with the -ReportType parameter as "Html" (Get-GPOReport -ReportType Html) and it works like a champ. I have a feeling the start up script for the session configuration is limiting too much and preventing the XML output from the Get-GPOReport cmdlet to be displayed. Not sure though.
Can anyone help me work through this, please?
PSSessionConfiguration Startup Script:
##--LOAD SESSION MODULES--## Import-Module ActiveDirectory Import-Module GroupPolicy ##--RESTRICT ACCESS TO ALL CMDLETS, VARIABLES, APPS FIRST--## Foreach ($command in Get-Command) {$command.Visibility = "private"} Foreach ($variable in Get-Variable) {$variable.Visibility = "private"} $ExecutionContext.SessionState.Applications.Clear() $ExecutionContext.SessionState.Scripts.Clear() $ExecutionContext.SessionState.LanguageMode = "NoLanguage" $InitialSessionState = [Management.Automation.Runspaces.InitialSessionState]::CreateRestricted("RemoteServer") Foreach ($proxy in ($InitialSessionState.Commands | Where {$_.Visibility -eq "Public"})) { $cmdlet = Get-Command -CommandType cmdlet -ErrorAction SilentlyContinue $proxy.Name If ($cmdlet) { $alias = Set-Alias "$($proxy.Name)" "$($cmdlet.ModuleName)\$($cmdlet.Name)" -PassThru $alias.Visibility = "Private" } Set-Item "function:global:$($proxy.Name)" $proxy.Definition } ##--GRANT ACCESS TO SPECIFIC CMDLETS--## $allowedCmdlets = @("Get-ADUser", "Get-ADGroup", "Get-ADGroupMember", "Get-GPO", "Get-GPOReport") Get-Command | Where-Object {$allowedCmdlets -contains $_.Name} | ForEach-Object {$_.Visibility = "Public"}