Set-WSManInstance fails in PowerShell 7 - powershell

I have PowerShell 5 and PowerShell 7 installed on my computer.
I open a PowerShell window as Administrator for each version.
PowerShell 5
PS C:\> Get-WSManInstance -ResourceURI winrm/config
cfg : http://schemas.microsoft.com/wbem/wsman/1/config
lang : en-US
MaxEnvelopeSizekb : 500
MaxTimeoutms : 60000
MaxBatchItems : 32000
MaxProviderRequests : 4294967295
Client : Client
Service : Service
Winrs : Winrs
PS C:\> Set-WSManInstance -ResourceURI winrm/config -ValueSet #{MaxEnvelopeSizekb="200"}
cfg : http://schemas.microsoft.com/wbem/wsman/1/config
lang : en-US
MaxEnvelopeSizekb : 200
MaxTimeoutms : 60000
MaxBatchItems : 32000
MaxProviderRequests : 4294967295
Client : Client
Service : Service
Winrs : Winrs
PowerShell 7 (7.2.5)
PS C:\> Get-WSManInstance -ResourceURI winrm/config
cfg : http://schemas.microsoft.com/wbem/wsman/1/config
lang : en-US
MaxEnvelopeSizekb : 200
MaxTimeoutms : 60000
MaxBatchItems : 32000
MaxProviderRequests : 4294967295
Client : Client
Service : Service
Winrs : Winrs
PS C:\> Set-WSManInstance -ResourceURI winrm/config -ValueSet #{MaxEnvelopeSizekb="200"}
Set-WSManInstance: The WS-Management service cannot process the request. The resource URI is missing or it has an incorrect format. Check the documentation or use the following command for information on how to construct a resource URI: "winrm help uris".
PS C:\>
The command I am using above is taken from the cmdlet Example 2 in the Powershell documentation.
Any ideas to why this is different in PowerShell 7?

Related

Test-NetConnection problem with ConstrainInterface

How check communication between two another interface different than data?
For example I want check communication TCP between two voice interfaces. NOT Ping.
Source IP is 10.116.69.215
Destination IP is 10.104.74.145
Previosly I tried:
Test-NetConnection -Port 5060 -ComputerName 10.104.74.145
But I receive only summary from interface data.
ComputerName : 10.104.74.145
RemoteAddress : 10.104.74.145
RemotePort : 5060
InterfaceAlias : VMAccess
SourceAddress : 10.116.64.202
PingSucceeded : False
PingReplyDetails (RTT) : 0 ms
TcpTestSucceeded : False
I tried modify cmdlet. So I began from
Get-NetAdapter
Name InterfaceDescription ifIndex Status
---- -------------------- ------- ------
BackupNet Microsoft Hyper-V Network Adapter #3 15 Up
VoiceNet Microsoft Hyper-V Network Adapter #4 17 Up
VMAccess Microsoft Hyper-V Network Adapter #2 14 Up
BootNet Microsoft Hyper-V Network Adapter 13 Not Present
Test-NetConnection 10.104.74.145 -Port 5060 -ConstrainInterface 17 -DiagnoseRouting -InformationLevel Detailed
but I receive:
Test-NetConnection : A parameter cannot be found that matches parameter name 'ConstrainInterface'.
At line:1 char:45
+ ... st-netconnection 10.102.73.135 -Port 5060 -ConstrainInterface 17 -Dia ...
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Test-NetConnection], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Test-NetConnection
What's wrong?
Can't use -port with that parameter set.
Test-NetConnection
[[-ComputerName] <String>]
[-DiagnoseRouting]
[-ConstrainSourceAddress <String>]
[-ConstrainInterface <UInt32>]
[-InformationLevel <String>]
[<CommonParameters>]

Test-WSMan fails with powershell

PS C:\Windows\system32> Test-WSMan x.x.xx.x
Test-WSMan : <f:WSManFault xmlns:f="http://schemas.microsoft.com/wbem/wsman/1/wsmanfault" Code="995" Machine="desktop-12"><f:Message>WS-Management cannot process the request. The operation
failed because of an HTTP error. The HTTP error (12152) is: The server returned an invalid or unrecognized response . </f:Message></f:WSManFault>
At line:1 char:1
+ Test-WSMan x.x.xx.x
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (x.x.xx.x:String) [Test-WSMan], InvalidOperationException
+ FullyQualifiedErrorId : WsManError,Microsoft.WSMan.Management.TestWSManCommand
I am failing to execute command from this laptop only. RDP connectivity is succeed on this laptop.
Below are output from some commands
PS C:\Windows\system32> winrm get winrm/config/service/auth
Auth
Basic = false
Kerberos = true
Negotiate = true
Certificate = false
CredSSP = false
CbtHardeningLevel = Relaxed
PS C:\Windows\system32> Get-NetConnectionProfile
Name : Unidentified network
InterfaceAlias : Ethernet 2
InterfaceIndex : 8
NetworkCategory : Public
IPv4Connectivity : NoTraffic
IPv6Connectivity : NoTraffic
Name : K32806
InterfaceAlias : Wi-Fi
InterfaceIndex : 17
NetworkCategory : Public
IPv4Connectivity : Internet
IPv6Connectivity : NoTraffic
PS C:\Windows\system32> Get-Item wsman:\localhost\client\trustedhosts
WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Client
Type Name SourceOfValue Value
---- ---- ------------- -----
System.String TrustedHosts x.x.xx.x
I have enabled Enable-PSRemoting -force, Restart-Service WinRM and tried Test-WSMan but not luck.Please help me to solve this problem.
Make sure your host is accessible from local machine and is not in private network if it is in private vpn connection will required.
Verify Host Ip is correct

How to use powershell.exe with -Command using a scriptblock and parameters

For reasons that should not impact the current question I need to run a script, with the definition and parameters outside the command, inside a different PowerShell instance, without using PSSessions, background jobs or files (I have working examples for PSSession, background jobs and .ps1 files and I'm aware they can replace what I'm trying to do, but I need a working example with powershell.exe -Command as well).
I looked at the help for powershell.exe, and it should support what I am trying to do, but I can't get it working with all that I need (script definition and parameters outside the command).
As a working example I have:
$abc = powershell.exe -WindowStyle Hidden -NonInteractive -Command {Invoke-Command -ScriptBlock {
param($a1,$a2)
$a1*6
$a2*5} -Argumentlist #(8,'abc')}
I need to be able to at least move the -ArgumentList outside the command, like:
$abc = powershell.exe -WindowStyle Hidden -NonInteractive -Command {Invoke-Command -ScriptBlock {
param($a1,$a2)
$a1*6
$a2*5} -Argumentlist #($args[0],$args[1])} -args #(8,'abc')
and even better have:
$script={
param($a1,$a2)
$a1*6
$a2*5}
$args=#(8,'abc')
$abc = powershell.exe -WindowStyle Hidden -NonInteractive -Command $script -args $args
I already looked at the following similar questions, but couldn't find what I needed:
How to run a Powershell script from the command line and pass a directory as a parameter
How can I Start-Process powershell.exe with some string splitter?
Not sure if this helps I added a few things to your original script and changed $args to $z and it seemed to work.
$script={
param($a1 =1 ,$a2 = 2)
$a1*6
$a2*5
test-connection -Count 2 www.google.com
Write-Output $a1
Write-Output $a2
}
$z=#(8,'abc')
$abc = powershell.exe -WindowStyle Hidden -NonInteractive -Command $script -args $z
$abc
48
abcabcabcabcabc
PSComputerName : ok
IPV4Address :1.1.1.4
IPV6Address :
__GENUS : 2
__CLASS : Win32_PingStatus
__SUPERCLASS :
__DYNASTY : Win32_PingStatus
__RELPATH : Win32_PingStatus.Address="www.google.com",BufferSize=32,NoFragmentation=FALSE,RecordRoute=0,ResolveAddressNames=FALSE,SourceRou
te="",SourceRouteType=0,Timeout=4000,TimestampRoute=0,TimeToLive=80,TypeofService=0
__PROPERTY_COUNT : 24
__DERIVATION : {}
__SERVER : ok
__NAMESPACE : root\cimv2
__PATH : \\ok\root\cimv2:Win32_PingStatus.Address="www.google.com",BufferSize=32,NoFragmentation=FALSE,RecordRoute=0,ResolveAddressName
s=FALSE,SourceRoute="",SourceRouteType=0,Timeout=4000,TimestampRoute=0,TimeToLive=80,TypeofService=0
Address : www.google.com
BufferSize : 32
NoFragmentation : False
PrimaryAddressResolutionStatus : 0
ProtocolAddress :1.1.1.4
ProtocolAddressResolved :
RecordRoute : 0
ReplyInconsistency : False
ReplySize : 32
ResolveAddressNames : False
ResponseTime : 19
ResponseTimeToLive : 252
RouteRecord :
RouteRecordResolved :
SourceRoute :
SourceRouteType : 0
StatusCode : 0
Timeout : 4000
TimeStampRecord :
TimeStampRecordAddress :
TimeStampRecordAddressResolved :
TimestampRoute : 0
TimeToLive : 80
TypeofService : 0
PSComputerName : ok
IPV4Address :1.1.1.4
IPV6Address :
__GENUS : 2
__CLASS : Win32_PingStatus
__SUPERCLASS :
__DYNASTY : Win32_PingStatus
__RELPATH : Win32_PingStatus.Address="www.google.com",BufferSize=32,NoFragmentation=FALSE,RecordRoute=0,ResolveAddressNames=FALSE,SourceRou
te="",SourceRouteType=0,Timeout=4000,TimestampRoute=0,TimeToLive=80,TypeofService=0
__PROPERTY_COUNT : 24
__DERIVATION : {}
__SERVER : ok
__NAMESPACE : root\cimv2
__PATH : \\ok\root\cimv2:Win32_PingStatus.Address="www.google.com",BufferSize=32,NoFragmentation=FALSE,RecordRoute=0,ResolveAddressName
s=FALSE,SourceRoute="",SourceRouteType=0,Timeout=4000,TimestampRoute=0,TimeToLive=80,TypeofService=0
Address : www.google.com
BufferSize : 32
NoFragmentation : False
PrimaryAddressResolutionStatus : 0
ProtocolAddress :1.1.1.4
ProtocolAddressResolved :
RecordRoute : 0
ReplyInconsistency : False
ReplySize : 32
ResolveAddressNames : False
ResponseTime : 21
ResponseTimeToLive : 252
RouteRecord :
RouteRecordResolved :
SourceRoute :
SourceRouteType : 0
StatusCode : 0
Timeout : 4000
TimeStampRecord :
TimeStampRecordAddress :
TimeStampRecordAddressResolved :
TimestampRoute : 0
TimeToLive : 80
TypeofService : 0
8
abc
This answer isn't 100% relevant to the original poster, because they are trying to run PowerShell from within PowerShell. I am trying to run it PowerShell from either a command prompt, or specifically WMI. A little background: the reason I'm trying to do this is because PowerShell remoting is not enabled on my target machine and I want to enable it. I can't use winrm because it requires user input. So, this works:
$x=Get-WmiObject -ComputerName "<computer name>" -Namespace "root\cimv2" -Class "Win32_Process" -List
$x.Create('C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command "& C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command {Enable-PSRemoting}"',$null,$null)
Results:
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 2
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ProcessId : 12508
ReturnValue : 0
PSComputerName :
I should probably post this in a different question, but this one came up on a google search for "how to pass scriptblock to powershell.exe", so I thought it would be useful here.

New-PSSession does not work locally

I am trying to connect to the localhost using New-PSSession.
I have
Configured WinRM using
winrm quickconfig
Enabled PS Remoting
Enable-PSRemoting
Added Trusted Host
Set-Item WSMan:\localhost\Client\TrustedHosts * -Force
There is an inbound rule on 8173 port on firewall.
Output of winrm:
PS C:\> winrm get winrm/config/listener?Address=*+Transport=HTTP
Listener
Address = *
Transport = HTTP
Port = 8173
Hostname
Enabled = true
URLPrefix = wsman
CertificateThumbprint
Listening on = 127.0.0.1
I am trying to run the following command:
New-PSSession -ConnectionUri http://localhost:8173/WSMAN
but I get this error:
[localhost] Processing data from remote server failed with the following error message:
Error with error code 14 occurred while calling method WSManPluginReceiveResult. For
more information, see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [], PSRemotingTransportException
+ FullyQualifiedErrorId : PSSessionOpenFailed
EDIT:
The only extra thing that I see is that the network is connected to public
$listenerport = "8173"
winrmwinrm create winrm/config/Listener?Address=*+Transport=HTTP "#{Port="$listenerport"}"
C:\>winrm get winrm/config
Config
MaxEnvelopeSizekb = 1039440
MaxTimeoutms = 60000
MaxBatchItems = 32000
MaxProviderRequests = 4294967295
Client
NetworkDelayms = 5000
URLPrefix = wsman
AllowUnencrypted = true
Auth
Basic = true
Digest = true
Kerberos = true
Negotiate = true
Certificate = true
CredSSP = false
DefaultPorts
HTTP = 8173
HTTPS = 5986
TrustedHosts = *
Service
RootSDDL = O:NSG:BAD:P(A;;GA;;;BA)(A;;GA;;;S-1-5-21-2458768215-3945602940-3262220185-1045)S:P(AU;FA;GA;;;WD)(AU;SA;GWGX;;;WD)
MaxConcurrentOperations = 4294967295
MaxConcurrentOperationsPerUser = 500
EnumerationTimeoutms = 60000
MaxConnections = 25
MaxPacketRetrievalTimeSeconds = 120
AllowUnencrypted = true
Auth
Basic = true
Kerberos = false
Negotiate = true
Certificate = true
CredSSP = false
CbtHardeningLevel = Relaxed
DefaultPorts
HTTP = 5985
HTTPS = 5986
IPv4Filter = *
IPv6Filter = *
EnableCompatibilityHttpListener = false
EnableCompatibilityHttpsListener = false
CertificateThumbprint
Winrs
AllowRemoteShellAccess = true
IdleTimeout = 180000
MaxConcurrentUsers = 5
MaxShellRunTime = 2147483647
MaxProcessesPerShell = 15
MaxMemoryPerShellMB = 150
MaxShellsPerUser = 5
PS C:\> Get-PSSessionConfiguration microsoft.powershell | fl *
xmlns : http://schemas.microsoft.com/wbem/wsman/1/config/PluginConfiguration
Name : Microsoft.PowerShell
Filename : %windir%\system32\pwrshplugin.dll
SDKVersion : 1
XmlRenderingType : text
lang : en-US
PSVersion : 2.0
ResourceUri : http://schemas.microsoft.com/powershell/Microsoft.PowerShell
SupportsOptions : true
ExactMatch : true
Capability : {Shell}
Permission :
Administrators group have permission as I see in the window popup (Set-PSSessionConfiguration -Name Microsoft.PowerShell -showSecurityDescriptorUI)
EDIT 2:
By process of elimination, we can rule out firewall as an issue, as you are only connecting to the loopback address (127.0.0.1). We can also rule out WinRM configuration which looks fine.
The error message suggests that TCP connection to http://localhost:8173/WSMAN is actually successful, but fault occurs while establishing PS session.
This points to Microsoft.PowerShell session configuration.
Looks like there is a discrepancy in the permissions you see when looking at
Set-PSSessionConfiguration -Name Microsoft.PowerShell -showSecurityDescriptorUI
and the permission actually assigned to Microsoft.PowerShell.
The output of
Get-PSSessionConfiguration microsoft.powershell | fl *
should have the "SecurityDescriptorSddl" and "Permission" proprieties listed. Like this:
Name : microsoft.powershell
Filename : %windir%\system32\pwrshplugin.dll
SDKVersion : 1
XmlRenderingType : text
lang : en-US
PSVersion : 2.0
ResourceUri : http://schemas.microsoft.com/powershell/microsoft.powershell
SupportsOptions : true
Capability : {Shell}
xmlns : http://schemas.microsoft.com/wbem/wsman/1/config/PluginConfiguration
Uri : http://schemas.microsoft.com/powershell/microsoft.powershell
ExactMatch : true
SecurityDescriptorSddl : O:NSG:BAD:P(A;;GA;;;BA)S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;;;WD)
Permission : BUILTIN\Administrators AccessAllowed
Try removing and reassigning these permissions.
EDIT:
Based on the information you have provided this is not the main problem. I have also noticed that you have a non standard "RootSDDL" in WinRM service settings.
RootSDDL = O:NSG:BAD:P(A;;GA;;;BA)S:P(AU;FA;GA;;;S-1-5-21-2458768215-3945602940-3262220185-1045)(AU;SA;GWGX;;;WD)
by default this should be
RootSDDL = O:NSG:BAD:P(A;;GA;;;BA)S:P(AU;FA;GA;;;WD)(AU;SA;GWGX;;;WD)
I have recreated this on the test VM and Remoting still works. So I had another look at your WinRM configuration ...
Solution
Your problem is this line
MaxEnvelopeSizekb = 1039440
By setting this value I can replicate the issue you have. I would suggest to set this to something more reasonable, or to default.
winrm set winrm/config '#{MaxEnvelopeSizekb="150"}'
Will fix your problem.
setting the following worked for me after I upgraded to computers from win 10 1607 to 1708
the update changed the following reg key to 1, and setting it back to 0 worked for me
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System
FilterAdministratorToken=dword:0
In a "strict" IPV4 environent, we had the same Problem as well.
The reason was: WinRM (and other services) seem to use the IpV6-Loopback-Address even if ipv6 is disabled everywhere. (Which is no Problem, except with WinRM)
Hence, if there is no WinRM-Listener for the IPV6-Loopback Adress - you can't connect to localhost, even if WinRM is working from a remote-host.

CommandNotFoundException: get-windowsoptionalfeature

I am using Powershell Version 2 on windows 7. I need to run the following command: get-windowsoptionalfeature
but when I run: get-command -Verb Get, the get-windowsoptionalfeature is not listed and as a result when I enter the command I receive an error stating that "get-windowsoptionalfeature" is not recognized as the name of a cmdlet, function.
Am I missing a dll or something?
get-windowsoptionalfeature is only applicable to Windows 8 & Server 2012.
try this in powershell console
PS C:\>$feature=Get-WmiObject -query "select * from Win32_OptionalFeature"
Now $feature is array of object of type ManagementObject.
to prove it try
PS C:\> $feature[0]
this is what I get.
__GENUS : 2
__CLASS : Win32_OptionalFeature
__SUPERCLASS : CIM_LogicalElement
__DYNASTY : CIM_ManagedSystemElement
__RELPATH : Win32_OptionalFeature.Name="OEMHelpCustomization"
__PROPERTY_COUNT : 6
__DERIVATION : {CIM_LogicalElement, CIM_ManagedSystemElement}
__SERVER : TTL001343
__NAMESPACE : root\cimv2
__PATH : \\TTL001343\root\cimv2:Win32_OptionalFeature.Name="OEMHelpCustomization"
Caption :
Description :
InstallDate :
InstallState : 2
Name : OEMHelpCustomization
Status :
You can get query specific objects as well.
example:
PS C:\>$feature=Get-WmiObject -query "select * from Win32_OptionalFeature where name = 'RemoteServerAdministrationTools-Roles-AD-Powershell'"
Now this will be single object not an array.
There's the Client Manager Module:
http://archive.msdn.microsoft.com/PSClientManager