Just for curiosity, it's not a 'I must have it', but how declare a tuple using system.tuple class in powershell?
I'm using powershell.exe.config to load framework 4.0 but I'm not able to create a tuple.
Trying this:
PS C:\ps1> $a = [System.Tuple``2]::Create( "pino", 34)
Chiamata al metodo non riuscita. [System.Tuple`2] non contiene un metodo denominato 'Create'.
In riga:1 car:31
+ $a = [System.Tuple``2]::Create <<<< ( "pino", 34)
+ CategoryInfo : InvalidOperation: (Create:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
sorry for Italian sample...
Thank you for help.
EDIT:
if i try:
PS C:\ps1> $a = [System.Tuple]::Create(34,"pino")
Impossibile trovare un overload per "Create" e il numero di argomenti: "2".
In riga:1 car:28
+ $a = [System.Tuple]::Create <<<< (34,"pino")
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
Here is a way
PS> $a = New-Object 'Tuple[string,int]'("Jack", 78)
PS> $a
Item1 Item2
----- -----
Jack 78
Another one
PS> $dpt = New-Object 'Tuple[string,string,int]'("Cantal", "Aurillac", 15)
PS> $dpt.Item2
Aurillac
------EDIT------
Recall
to see which CLR you are using, just use $PSVersionTable
PS C:\> $PSVersionTable
Name Value
---- -----
CLRVersion 2.0.50727.4959
BuildVersion 6.1.7600.16385
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
if you want PowerShell to start using CLR 4.0 you have to put the file powershell.exe.config in the folder $PSHOME (C:\Windows\System32\WindowsPowerShell\v1.0)
powershell.exe.config :
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319"/>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>
Result :
PS C:\Users\JPB> $PSVersionTable
Name Value
---- -----
PSVersion 2.0
PSCompatibleVersions {1.0, 2.0}
BuildVersion 6.1.7600.16385
PSRemotingProtocolVersion 2.1
WSManStackVersion 2.0
CLRVersion 4.0.30319.225
SerializationVersion 1.1.0.1
Related
This two-line PS script fails if debug tracing is set to level 2:
Set-PSDebug -Trace 2
$Process = Start-Process -FilePath ping -ArgumentList localhost -NoNewWindow -PassThru -Wait
It fails with this error:
Cannot convert value to type System.String.
At line:1 char:1
+ $Process = Start-Process -FilePath ping -ArgumentList localhost -NoNe ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromAnyTypeToString
If I set -Trace 1 or no tracing, it works. If I don't try to assign $Process in -Trace 2, it works.
I wouldn't expect Set-PSDebug to modify the execution environment, but that seems to be the case here. Is this expected behavior? Or perhaps a bug in PowerShell?
Version information:
PS C:\Users\Eric> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.19041.2364
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.19041.2364
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Cannot enable TLS1.2 on Windows 7SP1 with powershell 2.0 :
PS C:\Windows\system32> $PSVersionTable
Name Value
---- -----
CLRVersion 2.0.50727.8806
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
I guess P.Sv2.0 is using .NET v2.0 :
PS C:\Windows\system32> $PSVersionTable.CLRVersion
Major Minor Build Revision
----- ----- ----- --------
2 0 50727 8806
PS C:\Windows\system32> [System.Environment]::Version
Major Minor Build Revision
----- ----- ----- --------
2 0 50727 8806
Moreover, the following .NET versions are installed on this system :
PS C:\Windows\system32> Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse | Get-ItemProperty -Name Version, Release -ErrorAction 0 | where { $_.PSChildName -match '^(?!S)\p{L}'} | select Version, Release, PSChildName
Version Release PSChildName
------- ------- -----------
2.0.50727.5420 v2.0.50727
3.0.30729.5420 v3.0
3.0.4506.5420 Windows Communication Foundation
3.0.6920.5011 Windows Presentation Foundation
3.5.30729.5420 v3.5
4.8.03761 528049 Client
4.8.03761 528049 Full
4.0.0.0 Client
However, I cannot set TLSv1.2 with that configuration :
PS C:\Windows\system32> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Exception lors de la définition de « SecurityProtocol » : « Impossible de convertir la valeur Null en type « System.Net.SecurityProtocolType » en raison de valeurs d'énumération non valides. Spécifiez l'une des valeurs d'énumération suivantes et réessayez. Les valeurs d'énumération possibles sont « Ssl3, Tls ». »
Au niveau de ligne : 1 Caractère : 28
+ [Net.ServicePointManager]:: <<<< SecurityProtocol = [Net.SecurityProtocolType]::Tls12
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
PS C:\Windows\system32> [enum]::GetNames([Net.SecurityProtocolType])
Ssl3
Tls
PS C:\Windows\system32>
EDIT 0 : #mclayton Setting up [Net.ServicePointManager]::SecurityProtocol to the integer value of 3072 does not work either :
PS C:\Windows\system32> [Net.ServicePointManager]::SecurityProtocol = 3072
Exception lors de la définition de « SecurityProtocol » : « Impossible de convertir la valeur « 3072 » en type « System.Net.SecurityProtocolType » en raison de valeurs d'énumérati
on non valides. Spécifiez l'une des valeurs d'énumération suivantes et réessayez. Les valeurs d'énumération possibles sont « Ssl3, Tls ». »
Au niveau de ligne : 1 Caractère : 28
+ [Net.ServicePointManager]:: <<<< SecurityProtocol = 3072
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
Can you help me ?
To expand on #Abraham Zinala's comment, the Tls12 enumeration value wasn't added to the System.Net.SecurityProtocolType enum until Dot Net Framework version 4.5 (see https://learn.microsoft.com/en-us/dotnet/api/system.net.securityprotocoltype?view=netframework-4.5).
Your PowerShell 2.0 install is using a lower version than this, so it can't resolve the value of [Net.SecurityProtocolType]::Tls12.
What you can do instead is use the integer value of the enum:
# assign the magic number to a variable for clarity
$tls12 = 3072;
[Net.ServicePointManager]::SecurityProtocol = $tls12;
I noticed that the cmdlet Test-NetConnection was not installed on Server 2012. Since Server 2012 comes with PowerShell version 3 so I thought it might help to update to the latest version 5.1.
I did the update but the cmdlet Test-NetConnection is still not available.
Only Test-Connection is present, but I need Test-NetConnection to test ports.
How can I get Test-NetConnection now?
PS C:\Windows\system32> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.14409.1005
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14409.1005
CLRVersion 4.0.30319.34209
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
PS C:\Windows\system32> Get-Command Test-NetConnection
Get-Command : The term 'Test-NetConnection' 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:1 char:1
+ Get-Command Test-NetConnection
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Test-NetConnection:String) [Get-Command], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand
The availability of many cmdlets is tied to the Windows version, not the PowerShell version. If you can't upgrade your Windows version you can't have Test-NetConnection.
You could use a commandline port scanner like nmap or scanline for port tests, or you could connect to the port(s) yourself:
function Test-Port($server, $port) {
$client = New-Object Net.Sockets.TcpClient
try {
$client.Connect($server, $port)
$true
} catch {
$false
} finally {
$client.Dispose()
}
}
$ipaddress = "serverName"
$port = "portNum"
$connection = New-Object System.Net.Sockets.TcpClient($ipaddress, $port)
if ($connection.Connected) {
Write-Host "Success"
} else {
Write-Host "Failed"
}
I have two different Windows Server 2012 R2 machines (built at different times) and I want to check the LCM state in my PS script.
One server returns the LCMState from Get-DscLocalConfigurationManager, the other doesn't return the LCMState or any other LCM members.
Is this due to an older version of the WMF?
I had a look at https://serverfault.com/questions/627386/what-version-of-windows-management-framework-is-installed but both servers report the same PSVersion.
On the new working server:
PS C:\> Get-DscLocalConfigurationManager
ActionAfterReboot : ContinueConfiguration
AllowModuleOverwrite : False
CertificateID :
ConfigurationID :
ConfigurationMode : ApplyAndMonitor
ConfigurationModeFrequencyMins : 15
Credential :
DebugMode : {NONE}
DownloadManagerCustomData :
DownloadManagerName :
LCMCompatibleVersions : {1.0}
LCMState : Idle
LCMVersion : 1.0
RebootNodeIfNeeded : False
RefreshFrequencyMins : 30
RefreshMode : PUSH
PSComputerName :
PS C:\> (get-module PSDesiredStateConfiguration).Version
Major Minor Build Revision
----- ----- ----- --------
1 0 -1 -1
PS C:\> $PSVersionTable
Name Value
---- -----
PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.42000
BuildVersion 6.3.9600.18728
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2
On the older server the LCM members are not present:
PS C:\> Get-DscLocalConfigurationManager
AllowModuleOverwrite : False
CertificateID :
ConfigurationID :
ConfigurationMode : ApplyAndMonitor
ConfigurationModeFrequencyMins : 30
Credential :
DownloadManagerCustomData :
DownloadManagerName :
RebootNodeIfNeeded : False
RefreshFrequencyMins : 15
RefreshMode : PUSH
PSComputerName :
PS C:\> (Get-Module PSDesiredStateConfiguration).Version
Major Minor Build Revision
----- ----- ----- --------
1 0 -1 -1
PS C:\> $PSVersionTable
Name Value
---- -----
PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.34003
BuildVersion 6.3.9600.16394
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2
How do I obtain the LCM state on the older server?
You have a different build on the first system (6.3.9600.18728). WMF 4.0 had a November 2014 update where some of these were introduced. So, the second system is missing KB3000850
I am trying to create a powershell script (version 2.0) to send inline images, as explained here. But something does not seem to work, or is maybe outdated or incorrect.
The following is a complete test script
$msg = new-object Net.Mail.MailMessage
$image = "testImage.png"
$imageFull = "D:\Testing\testImage.png"
$attachment = New-Object System.Net.Mail.Attachment –ArgumentList $imageFull
$body=#"
<html>
<body>
<img src="cid:$image">
</body>
</html>
"#
which only uses an example image, and creates the following error:
The string starting:
At D:\Testing\Data\Powershell\LoadRunner\LRtestError.ps1:14 char:1
+ <<<< "#
is missing the terminator: ".
At D:\Testing\Data\Powershell\LoadRunner\LRtestError.ps1:16 char:1
+ <<<<
+ CategoryInfo : ParserError: (#
:String) [], ParseException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
What is going on? Why is this error created? How to fix it?
NOTE:
If you comment out the System.Net.Mail.Attachment line, no error is created!!
Used OS: Windows Server 2008 R2 Standard (64-bit)
Full version table:
$psversiontable:
Name Value
---- -----
CLRVersion 2.0.50727.5485
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
Make sure that #" are the first characters, and "# are last characters on the line. And that assigning the variable has it's own line $body=#". The code runs fine for me.
More information about these "Here-Strings" can be found here
$msg = new-object Net.Mail.MailMessage
$image = "testImage.png"
$imageFull = "D:\Testing\testImage.png"
$attachment = New-Object System.Net.Mail.Attachment –ArgumentList $imageFull
$body=#"
<html>
<body>
<img src="cid:$image">
</body>
</html>
"#