Psedit on a item created with the New-Item cmdlet throws exception - powershell

When I create a new file using the New-Item cmdlet and want to edit it within PowerShell ISE using psedit I receive the following exception:
Exception calling "Add" with "1" argument(s): "Object reference not set to an instance of an object."
At line:7 char:13
+ $psISE.CurrentPowerShellTab.Files.Add($_.FullName) > $nul ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NullReferenceException
Code:
New-Item readme.txt
psedit readme.txt
PsVersionTable:
Name Value
---- -----
PSVersion 5.0.10586.494
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.10586.494
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Is this a known Bug? Any workaround?

Running the following works without an issue and opens a new tab in the ISE with the readme.txt:
New-Item Readme.txt -ItemType file
psedit Readme.txt
PSVersiontable:
Name Value
---- -----
PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.34209
BuildVersion 6.3.9600.17400
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2

It seems like this is a PowerShell V5 bug since its working fine on PowerShell V4 (see the comment from Micky Balladelli). A workaround is to create the file using the Out-File cmdlet:
'' | Out-File readme.txt
psedit readme.txt

Related

Unable to Unzip Folder when invoked on a remote PC

I'm Attempting to Unzip A folder located on a shared drive to the root of C:\ on a remote PC but keep getting errors, how do i correct this Powershell?
$Computers = "LN-T48-PF11BL57"
Invoke-Command -Computername $Computers -ScriptBlock {
Expand-Archive -LiteralPath '\\LNAPPS\APPS\Adobe iManage Fix\Program Files (x86).zip' -DestinationPath C:\ -Force } -Verbose
Write-Host "Enter to Exit"
This returns the following error:
A positional parameter cannot be found that accepts argument '\\LNAPPS\APPS\Adobe iManage Fix\Program Files (x86).zip'.
+ CategoryInfo : InvalidArgument: (:) [Expand-Archive], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Expand-Archive
+ PSComputerName : LN-T48-PF11BL57
A few things here:
Remember Windows balks are dropping stuff in the root c:.
So, are we to assume that \LNApps is a server name and \APPS is a
folder share configured on that server?
Lastly, unless that server is running PowerShell v5, that
Expand-Archive cmdlet is not there.
Hence this...
: InvalidArgument: (:) [Expand-Archive], ParameterBindingException
(Get-CimInstance -ClassName CIM_OperatingSystem).Caption
Microsoft Windows Server 2012 R2 Standard
$PSVersionTable
Name Value
---- -----
PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.42000
BuildVersion 6.3.9600.19170
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2
Get-Command -Name '*Expand-archive*'
# No results
(Get-CimInstance -ClassName CIM_OperatingSystem).Caption
Microsoft Windows 10 Pro
$PSVersionTable
Name Value
---- -----
PSVersion 5.1.17763.316
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.17763.316}
BuildVersion 10.0.17763.316
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Get-Command -Name '*Expand-archive*'
CommandType Name Version Source
----------- ---- ------- ------
Function Expand-Archive 1.0.1.0 Microsoft.PowerShell.Archive
If that cmdlet is not there, you need to use the .Net namespace, System.IO.Compression.FileSystem, to deal with this use case.
can be used to either compress or extract files using this class. The
following example will compress the files stored in the c:\testing
folder:
Add-Type -Assembly 'System.IO.Compression.FileSystem'
[System.IO.Compression.ZipFile]::CreateFromDirectory('c:\testing', 'c:\testing.zip','Optimal',$false)
When you want to extract files, use the ExtractToDirectory method:
[System.IO.Compression.ZipFile]::ExtractToDirectory('c:\testing.zip', 'c:\newtest')

write-host -NoNewline parameter is ignored with powershell 5.1

Starting with WMF 5.1 the -NoNewLine option for Write-Host is essentially ignored.
Simple example:
PS C:\> Write-Host "no newline test " -NoNewline
Current Behaviour:
no newline test
PS C:\>
Expected Behavior
no newline test PS C:\>
PS version Table is as follows :
Name Value
---- -----
PSVersion 5.1.14409.1012
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14409.1012
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
As per Microsoft docs for powershell 5.1, Next Prompt should come in the same line after output of Write-Host "no newline test" -NoNewline statement.
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-host?view=powershell-5.1
Any idea why it is not functioning as expected?

Server 2012 R2 missing PSReadline cmdlets?

Works on Win 10 but on server 2012 R2 its not there.
PS> Get-PSReadlineKeyHandler
Get-PSReadlineKeyHandler : The term 'Get-PSReadlineKeyHandler' 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-PSReadlineKeyHandler
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-PSReadlineKeyHandler:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Can confirm I'm running v5
PS> $PSVersionTable
Name Value
---- -----
PSVersion 5.0.10586.117
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.10586.117
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
You can install it from the PowerShell gallery. But I believe you are correct that Windows 10 is the only OS that installs the module by default.
Find-Module psreadline | Install-Module

powershell remote import module error

I have a problem with this code
Invoke-Command -ComputerName $CSC_SERVER -ScriptBlock {
Import-Module ("C:\Build_v.6.1.0.9\Tools\CommonComponents.BuildCommands.dll")
}
Error:
Could not load file or assembly "file: /// C: \ Build V.6.1.0.9 \ Tools \ CommonComponents.BuildCommands.dll" or one of their dependent components. The assembly created in a later version of the runtime than the current, and can not be loaded.
When I go to the server via RDP and try to load this module everything is OK.
how to fix this problem?
UPD
> Invoke-Command -ComputerName $CSC_SERVER -ScriptBlock {$PSVersionTable}
Name Value
---- -----
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
PSCompatibleVersions {1.0, 2.0}
CLRVersion 2.0.50727.5420
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
And same via local session on remove server
> $PSVersionTable
Name Value
---- -----
PSVersion 2.0
PSCompatibleVersions {1.0, 2.0}
BuildVersion 6.1.7601.17514
CLRVersion 4.0.30319.17929
WSManStackVersion 2.0
PSRemotingProtocolVersion 2.1
SerializationVersion 1.1.0.1
Now I see that difference in CLRVersion. Powershell remoting uses CLR version 2.0. What is the reason for this behavior? And how to change it to version 4.0?

PS Remoting Protocol Version Mismatch

I have a disconnected session on Win7 Embedded SP1 32 bit station. I am trying to connect to that session. When I attempt to connect I get an error that suggests PSRemotingProtocolVersion mismatch. I can see my Windows 8.1 station has version 2.3 and the Win7 station has 2.2. How can I correct this issue so I can connect to the disconnected session?
Here is excerpts from my session...
PS C:\> Get-PSSession ef-pos-01.faculty.example.org
Id Name ComputerName State ConfigurationName Availability
-- ---- ------------ ----- ----------------- ------------
1 Session3 ef-pos-01.fa... Disconnected Microsoft.PowerShell None
PS C:\> Get-Pssession ef-pos-01.faculty.example.org | Connect-PSSession
Connect-PSSession : The connect operation failed for session Session3 with the following error message: Connecting to remote server
ef-pos-01.faculty.example.org failed with the following error message : The server that is running Windows PowerShell does not support connect operations on
the protocolversion 2.3 that is negotiated by the client computer. Make sure the client computer is compatible with the build 6.3.9600.16406 and the protocol
version 2.2 of Windows PowerShell. For more information, see the about_Remote_Troubleshooting Help topic.
At line:1 char:46
+ Get-Pssession ef-pos-01.faculty.example.org | Connect-PSSession
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Connect-PSSession], RuntimeException
+ FullyQualifiedErrorId : -2141974621,PSSessionConnectFailed,Microsoft.PowerShell.Commands.ConnectPSSessionCommand
PS C:\> Enter-PSSession ef-pos-01.faculty.example.org
[ef-pos-01.faculty.example.org]: PS C:\Users\mike.mackenna\Documents> $PSVersiontable
Name Value
---- -----
PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.17929
BuildVersion 6.3.9600.16406
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2
[ef-pos-01.faculty.example.org]: PS C:\Users\mike.mackenna\Documents> exit
PS C:\> $PSversionTable
Name Value
---- -----
PSVersion 5.0.10105.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.34209
BuildVersion 10.0.10105.0
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
I had this problem connecting Windows 10 (PS 5) to Server 2012 (PS 3). I could create the session and do whatever, but couldn't reconnect to it, even from the server. Likewise I could create and reconnect on the server (to localhost) but attempting to connect from my computer failed and broke the session.
I resolved this by installing WMF5 on the server to bring it up to PowerShell 5, is now running smoothly.