Function pipeline support, can't bind argument to parameter - powershell

I created following simple funcion, where the parameter supports input from pipeline:
function Write-Log {
[CmdletBinding()]
param (
# Lines to log
[Parameter(Mandatory , ValueFromPipeline )]
[string[]]
$PipeLineLoglines
)
process {
Write-Host $_
}
}
If I call the function via e.g. :
"test1", "test2" | Write-Log
It works as expected, but if I'm forwarding an empty string down the pipeline I get the folowing error:
C:\> "test1", "", "test2" | Write-Log
test1
Write-Log : Cannot bind argument to parameter 'PipeLineLoglines' because it is an empty string.
At line:1 char:24
+ "test1", "", "test2" | Write-Log
+ ~~~~~~~~~
+ CategoryInfo : InvalidData: (:String) [Write-Log], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Write-Log
test2
I'm using PowerShell.Core:
C:\> $PSVersionTable
Name Value
---- -----
PSVersion 6.2.0
PSEdition Core
GitCommitId 6.2.0
OS Microsoft Windows 10.0.17763
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Can someone explain why an empty string causes this error?
Thx

It's because the parameter is Mandatory, and it's considered that an empty string did not satisfy that condition. Given that there are specific validation attributes to test for that, I don't like this behavior, but that's the way it is.
There is an attribute that can help you though, [AllowEmptyString()]:
function Write-Log {
[CmdletBinding()]
param (
# Lines to log
[Parameter(Mandatory , ValueFromPipeline )]
[AllowEmptyString()]
[string[]]
$PipeLineLoglines
)
process {
Write-Host $_
}
}

Related

"Set-PSDebug -Trace 2" causes a variable assignment to fail

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

Powershell ISE VSCODE Invoke-Webrequest difference

I am try to download the nuget.exe via Invoke-WebRequest inside a function.
If i use Windows Powershell ISE everything works fine.
$request = Invoke-WebRequest -Uri "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" -UseBasicParsing -verbose
VERBOSE: GET https://dist.nuget.org/win-x86-commandline/latest/nuget.exe with 0-byte payload
VERBOSE: received 7088048-byte response of content type application/x-msdownload
If i use VSCODE Powershell extension i get.
$request = Invoke-WebRequest -Uri "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" -UseBasicParsing -verbose
VERBOSE: GET https://dist.nuget.org/win-x86-commandline/latest/nuget.exe with 0-byte payload
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At line:1 char:12
+ $request = Invoke-WebRequest -Uri "https://dist.nuget.org/win-x86-com ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
I already tried
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
[System.Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls13 -bor [Net.SecurityProtocolType]::Ssl3
$PSVersionTable in ISE and VSCODE extension is identical
Name Value
---- -----
PSVersion 5.1.19041.1682
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.19041.1682
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Is there a way to analyze/debug the difference? There must be some kind of difference in this environments.
Other download variants besides Invoke-WebRequest won't work for me, because i need the $request answer to phrase the header in some variants
The error was a previous call to...
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
Setting the callback to $true won't work and screw things up.
-SkipCertificateCheck is not availible in PSVersion 5.1.19041.1682
Added some C# code to Set the ServerCertificateValidationCallback to true if needed.
function Private-SslVerification {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseApprovedVerbs", "")]
param()
if (-not ([System.Management.Automation.PSTypeName]"SslVerification").Type)
{
Add-Type -TypeDefinition #"
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public static class SslVerification
{
private static bool ValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }
public static void Disable() { System.Net.ServicePointManager.ServerCertificateValidationCallback = ValidationCallback; }
public static void Enable() { System.Net.ServicePointManager.ServerCertificateValidationCallback = null; }
}
"#
}
}
function Private-Disable-SslVerification
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseApprovedVerbs", "")]
param()
Private-SslVerification
[SslVerification]::Disable()
}
function Private-Enable-SslVerification
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseApprovedVerbs", "")]
param()
Private-SslVerification
[SslVerification]::Enable()
}

PowerShell cmdlet Test-NetConnection not available

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"
}

Passing PSRemotingJob Object as Parameter in Powershell 4.0

I'm writing a Powershell Cmdlet for which I need to pass in a PSRemotingJob
object as a parameter. The MCVE follows:
function My-Cmdlet {
[CmdletBinding()]
Param(
[Parameter(Position=0,
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[PSRemotingJob[]]$Job
)
BEGIN {}
PROCESS {
ForEach ($j in $Job) {
$j
}
}
END {}
}
The issue is that when I pass a job into the cmdlet, I get an error, as follows:
PS C:\Temp> Invoke-Command -AsJob -CN svr001 -Command {Start-Sleep 10} | My-Cmdlet
My-Cmdlet : Unable to find type [PSRemotingJob]. Make sure that the assembly that contains this type is loaded.
At line:1 char:63
+ Invoke-Command -AsJob -CN svr001 -Command {Start-Sleep 10} | My-Cmdlet
+ ~~~~~~~~~
+ CategoryInfo : InvalidOperation: (PSRemotingJob:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
PS C:\Temp>
I realize that this should be a simple matter of substituting the correct object
type or fully-qualified object, but I've also tried using
[System.Management.Automation.PSRemotingJob] with the same results.
I'm using Powershell 4.0.
System.Management.Automation.PSRemotingJob is not public type and thus can not be expressed in PowerShell type syntax. But you can use its base public type instead: [System.Management.Automation.Job].

Add-PsSnapin WebAdministration in Windows7

I want to use PowerShell to administer my IIS7 on Windows7 64 bits.
I try install IIS7 Powershell snap-in http://www.iis.net/download/PowerShell
I "Run as administrator", then typing the command listed below: msiexec /I iis7psprov_x64.msi but I get this error message: "The PowerShell snap-in is part of Windows Operating System. Please install via Programs and Features or Server Manager"
I think this particular feature is not necessary to load the WebAdministration module but I active all options in "Programs and features |
turn Windows features on or off | IIS | Web Management Tools | IIS Management Scripts and Tools
I do those checks:
1.) Power-Shell is installed
PS C:\Program Files\IIS> $Host.Version
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
PS C:\Program Files\IIS> $PSVersionTable
Name Value
---- -----
CLRVersion 2.0.50727.5448
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
PS C:\Program Files\IIS> get-host
Name : ConsoleHost
Version : 2.0
InstanceId : 445ad8f5-87fc-48f7-b010-f7faf948b86c
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : es-ES
CurrentUICulture : es-ES
PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace
2.) Set-ExecutionPolicy -ExecutionPolicy {Your Execution Policy}
PS C:\Program Files\IIS> get-ExecutionPolicy
Unrestricted
3.)Import-Module WebAdministration
PS C:\Program Files\IIS> Import-Module WebAdministration
PS C:\Program Files\IIS>
4.) C:\Windows\System32\WindowsPowerShell\v1.0\Modules\WebAdministration path exists
5.) Modules and Snap-in
PS C:\Program Files\IIS> get-module -listavailable
ModuleType Name ExportedCommands
---------- ---- ----------------
Manifest AppLocker {}
Manifest BitsTransfer {}
Manifest PSDiagnostics {}
Manifest TroubleshootingPack {}
Manifest WebAdministration {}
PS C:\Program Files\IIS> Get-PSSnapin -Registered
Name : SqlServerCmdletSnapin100
PSVersion : 2.0
Description : This is a PowerShell snap-in that includes various SQL Server cmdlets.
Name : SqlServerProviderSnapin100
PSVersion : 2.0
Description : SQL Server Provider
Name : TfsBPAPowerShellSnapIn
PSVersion : 2.0
Description : This is a PowerShell snap-in that includes Team Foundation Server cmdlets.
PS C:\Program Files\IIS> get-pssnapin
Name : Microsoft.PowerShell.Diagnostics
PSVersion : 2.0
Description : Este complemento de Windows PowerShell contiene cmdlets de Eventos de Windows y de contador de rendimient
o.
Name : Microsoft.WSMan.Management
PSVersion : 2.0
Description : Este complemento de Windows PowerShell contiene cmdlets (como Get-WSManInstance y Set-WSManInstance) que
usa el host de Windows PowerShell para administrar operaciones WSMan.
Name : Microsoft.PowerShell.Core
PSVersion : 2.0
Description : Este complemento de Windows PowerShell contiene cmdlets usados para administrar los componentes de Window
s PowerShell.
Name : Microsoft.PowerShell.Utility
PSVersion : 2.0
Description : Este complemento de Windows PowerShell contiene cmdlets de utilidad que sirven para manipular datos.
Name : Microsoft.PowerShell.Host
PSVersion : 2.0
Description : Este complemento de Windows PowerShell contiene cmdlets (como Start-Transcript y Stop-Transcript) proporc
ionados para su uso con el host de la consola de Windows PowerShell.
Name : Microsoft.PowerShell.Management
PSVersion : 2.0
Description : El complemento Windows PowerShell contiene cmdlets de administración para administrar los componentes de
Windows.
Name : Microsoft.PowerShell.Security
PSVersion : 2.0
Description : Este complemento de Windows PowerShell contiene varios cmdlets para la administración de la seguridad de
Windows PowerShell.
PS C:\Program Files\IIS> [System.Reflection.Assembly]::LoadFrom( "C:\windows\system32\inetsrv\Microsoft.Web.Administration.dll" )
GAC Version Location
--- ------- --------
True v2.0.50727 C:\Windows\assembly\GAC_MSIL\Microsoft.Web.Administration\7.0.0.0__31bf3856ad364e35\Microsoft....
PS C:\Program Files\IIS> (New-Object Microsoft.Web.Administration.ServerManager)
ApplicationDefaults : Microsoft.Web.Administration.ApplicationDefaults
ApplicationPoolDefaults : Microsoft.Web.Administration.ApplicationPoolDefaults
ApplicationPools : {DefaultAppPool}
SiteDefaults : Microsoft.Web.Administration.SiteDefaults
Sites : {Default Web Site}
VirtualDirectoryDefaults : Microsoft.Web.Administration.VirtualDirectoryDefaults
WorkerProcesses : {}
PS C:\Program Files\IIS> (New-Object Microsoft.Web.Administration.ServerManager).Sites
ApplicationDefaults : Microsoft.Web.Administration.ApplicationDefaults
Applications : {Default Web Site/}
Bindings : {[http] *:80:}
Id : 1
Limits : Microsoft.Web.Administration.SiteLimits
LogFile : Microsoft.Web.Administration.SiteLogFile
Name : Default Web Site
ServerAutoStart : True
State : Started
TraceFailedRequestsLogging : Microsoft.Web.Administration.SiteTraceFailedRequestsLogging
VirtualDirectoryDefaults : Microsoft.Web.Administration.VirtualDirectoryDefaults
Attributes : {name, id, serverAutoStart, state}
ChildElements : {bindings, limits, logFile, traceFailedRequestsLogging...}
ElementTagName : site
IsLocallyStored : True
Methods : {Start, Stop}
RawAttributes : {[name, Default Web Site], [id, 1], [serverAutoStart, True], [state, 1]}
Schema : Microsoft.Web.Administration.ConfigurationElementSchema
6.) I try in ps1 file
$succeeded = import-module WebAdministration
Write-Host $succeeded
if (($succeeded -ne $null) -and ($succeeded.GetType() -eq [System.Exception]))
{
#Could not import, trying to snapin
add-pssnapin WebAdministration
}
$succeeded is null, and "add-pssnapin WebAdministration" produces error message: Add-PSSnapin : The Windows PowerShell snap-in 'WebAdministration' is not installed on this machine.
PS C:\Program Files\IIS> Add-PSSnapin WebAdministration
Add-PSSnapin : El complemento WebAdministration de Windows PowerShell no está instalado en este equipo
En línea: 1 Carácter: 13
+ Add-PSSnapin <<<< WebAdministration
+ CategoryInfo : InvalidArgument: (WebAdministration:String) [Add-PSSnapin], PSArgum
+ FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand
any suggestions about it ? Why fails Add-PSSnapin WebAdministration ?
UPDATE:
I'm confused when exactly do I use Add-PSSnapin and when do I use Import-Module
http://social.technet.microsoft.com/Forums/en/ITCG/thread/3e1ca6e1-d197-4c04-a145-1e922814a618
surely, WebAdministration is only Module, and has'nt PSSnapin. May be...
References:
PowerShell IIS Snapin
Cannot install Powershell Snap-in
http://www.iis.net/download/PowerShell
http://learn.iis.net/page.aspx/429/installing-the-iis-powershell-snap-in/
http://learningpcs.blogspot.com.es/2010/08/powershell-iis-7-webadministration.html
Versión de PowerShell instalada
Determine installed PowerShell version
Here is the function I have in my profile.ps1 for loading the WebAdmin module. It covers IIS 7 and 7.5
# Web administration is loaded as a module on Windows 2008 R2 but as a set of snapins
# for Windows 2008 (not R2)
function Util-Load-Web-Administration
{
$ModuleName = "WebAdministration"
$ModuleLoaded = $false
$LoadAsSnapin = $false
if ($PSVersionTable.PSVersion.Major -ge 2)
{
if ((Get-Module -ListAvailable | ForEach-Object {$_.Name}) -contains $ModuleName)
{
Import-Module $ModuleName
if ((Get-Module | ForEach-Object {$_.Name}) -contains $ModuleName)
{ $ModuleLoaded = $true } else { $LoadAsSnapin = $true }
}
elseif ((Get-Module | ForEach-Object {$_.Name}) -contains $ModuleName)
{ $ModuleLoaded = $true } else { $LoadAsSnapin = $true }
}
else
{ $LoadAsSnapin = $true }
if ($LoadAsSnapin)
{
try
{
if ((Get-PSSnapin -Registered | ForEach-Object {$_.Name}) -contains $ModuleName)
{
if ((Get-PSSnapin -Name $ModuleName -ErrorAction SilentlyContinue) -eq $null)
{ Add-PSSnapin $ModuleName }
if ((Get-PSSnapin | ForEach-Object {$_.Name}) -contains $ModuleName)
{ $ModuleLoaded = $true }
}
elseif ((Get-PSSnapin | ForEach-Object {$_.Name}) -contains $ModuleName)
{ $ModuleLoaded = $true }
}
catch
{
Write-Error "`t`t$($MyInvocation.InvocationName): $_"
Exit
}
}
}