Skip to content

Commit eedfad2

Browse files
authored
Fix issue where MS Dynamics CRM (PowerShell#686)
* Fix issue where MS Dynamics CRM redefines set-content, breaks Start-PSES Fixes PowerShell/vscode-powershell#1331 I wish we didn't have to module qualify command names like this but we've been bitten several times now by overridden commands. * Module qualify more commands I'm, for the moment, drawing the line at the *-Object commands. I really hate having to do this but I hate dealing with externally inflicted bugs even more.
1 parent b9c428a commit eedfad2

File tree

5 files changed

+61
-51
lines changed

5 files changed

+61
-51
lines changed

module/PowerShellEditorServices.VSCode/PowerShellEditorServices.VSCode.psm1

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
#
55

66
if (!$PSVersionTable.PSEdition -or $PSVersionTable.PSEdition -eq "Desktop") {
7-
Add-Type -Path "$PSScriptRoot/bin/Desktop/Microsoft.PowerShell.EditorServices.VSCode.dll"
7+
Microsoft.PowerShell.Utility\Add-Type -Path "$PSScriptRoot/bin/Desktop/Microsoft.PowerShell.EditorServices.VSCode.dll"
88
}
99
else {
10-
Add-Type -Path "$PSScriptRoot/bin/Core/Microsoft.PowerShell.EditorServices.VSCode.dll"
10+
Microsoft.PowerShell.Utility\Add-Type -Path "$PSScriptRoot/bin/Core/Microsoft.PowerShell.EditorServices.VSCode.dll"
1111
}
1212

1313
if ($psEditor -is [Microsoft.PowerShell.EditorServices.Extensions.EditorObject]) {
@@ -17,6 +17,6 @@ else {
1717
Write-Verbose '$psEditor object not found in the session, components will not be registered.'
1818
}
1919

20-
Get-ChildItem -Path $PSScriptRoot\Public\*.ps1 -Recurse | ForEach-Object {
20+
Microsoft.PowerShell.Management\Get-ChildItem -Path $PSScriptRoot\Public\*.ps1 -Recurse | ForEach-Object {
2121
. $PSItem.FullName
2222
}

module/PowerShellEditorServices/Commands/PowerShellEditorServices.Commands.psm1

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
44
#
55

6-
Import-LocalizedData -BindingVariable Strings -FileName Strings -ErrorAction Ignore
6+
Microsoft.PowerShell.Utility\Import-LocalizedData -BindingVariable Strings -FileName Strings -ErrorAction Ignore
77

8-
Get-ChildItem -Path $PSScriptRoot\Public\*.ps1 | ForEach-Object {
8+
Microsoft.PowerShell.Management\Get-ChildItem -Path $PSScriptRoot\Public\*.ps1 | ForEach-Object {
99
. $PSItem.FullName
1010
}
1111

12-
Export-ModuleMember -Function *-*
12+
Microsoft.PowerShell.Core\Export-ModuleMember -Function *-*

module/PowerShellEditorServices/PowerShellEditorServices.psm1

+29-20
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
#
55

66
if (!$PSVersionTable.PSEdition -or $PSVersionTable.PSEdition -eq "Desktop") {
7-
Add-Type -Path "$PSScriptRoot/bin/Desktop/Microsoft.PowerShell.EditorServices.dll"
8-
Add-Type -Path "$PSScriptRoot/bin/Desktop/Microsoft.PowerShell.EditorServices.Host.dll"
7+
Microsoft.PowerShell.Utility\Add-Type -Path "$PSScriptRoot/bin/Desktop/Microsoft.PowerShell.EditorServices.dll"
8+
Microsoft.PowerShell.Utility\Add-Type -Path "$PSScriptRoot/bin/Desktop/Microsoft.PowerShell.EditorServices.Host.dll"
99
}
1010
else {
11-
Add-Type -Path "$PSScriptRoot/bin/Core/Microsoft.PowerShell.EditorServices.dll"
12-
Add-Type -Path "$PSScriptRoot/bin/Core/Microsoft.PowerShell.EditorServices.Protocol.dll"
13-
Add-Type -Path "$PSScriptRoot/bin/Core/Microsoft.PowerShell.EditorServices.Host.dll"
11+
Microsoft.PowerShell.Utility\Add-Type -Path "$PSScriptRoot/bin/Core/Microsoft.PowerShell.EditorServices.dll"
12+
Microsoft.PowerShell.Utility\Add-Type -Path "$PSScriptRoot/bin/Core/Microsoft.PowerShell.EditorServices.Protocol.dll"
13+
Microsoft.PowerShell.Utility\Add-Type -Path "$PSScriptRoot/bin/Core/Microsoft.PowerShell.EditorServices.Host.dll"
1414
}
1515

1616
function Start-EditorServicesHost {
@@ -75,10 +75,14 @@ function Start-EditorServicesHost {
7575
)
7676

7777
$editorServicesHost = $null
78-
$hostDetails = New-Object Microsoft.PowerShell.EditorServices.Session.HostDetails @($HostName, $HostProfileId, (New-Object System.Version @($HostVersion)))
78+
$hostDetails =
79+
Microsoft.PowerShell.Utility\New-Object Microsoft.PowerShell.EditorServices.Session.HostDetails @(
80+
$HostName,
81+
$HostProfileId,
82+
(Microsoft.PowerShell.Utility\New-Object System.Version @($HostVersion)))
7983

8084
$editorServicesHost =
81-
New-Object Microsoft.PowerShell.EditorServices.Host.EditorServicesHost @(
85+
Microsoft.PowerShell.Utility\New-Object Microsoft.PowerShell.EditorServices.Host.EditorServicesHost @(
8286
$hostDetails,
8387
$BundledModulesPath,
8488
$EnableConsoleRepl.IsPresent,
@@ -87,15 +91,19 @@ function Start-EditorServicesHost {
8791
$FeatureFlags)
8892

8993
# Build the profile paths using the root paths of the current $profile variable
90-
$profilePaths = New-Object Microsoft.PowerShell.EditorServices.Session.ProfilePaths @(
91-
$hostDetails.ProfileId,
92-
[System.IO.Path]::GetDirectoryName($profile.AllUsersAllHosts),
93-
[System.IO.Path]::GetDirectoryName($profile.CurrentUserAllHosts));
94+
$profilePaths =
95+
Microsoft.PowerShell.Utility\New-Object Microsoft.PowerShell.EditorServices.Session.ProfilePaths @(
96+
$hostDetails.ProfileId,
97+
[System.IO.Path]::GetDirectoryName($profile.AllUsersAllHosts),
98+
[System.IO.Path]::GetDirectoryName($profile.CurrentUserAllHosts))
9499

95100
$editorServicesHost.StartLogging($LogPath, $LogLevel);
96101

97-
$languageServiceConfig = New-Object Microsoft.PowerShell.EditorServices.Host.EditorServiceTransportConfig
98-
$debugServiceConfig = New-Object Microsoft.PowerShell.EditorServices.Host.EditorServiceTransportConfig
102+
$languageServiceConfig =
103+
Microsoft.PowerShell.Utility\New-Object Microsoft.PowerShell.EditorServices.Host.EditorServiceTransportConfig
104+
105+
$debugServiceConfig =
106+
Microsoft.PowerShell.Utility\New-Object Microsoft.PowerShell.EditorServices.Host.EditorServiceTransportConfig
99107

100108
if ($Stdio.IsPresent) {
101109
$languageServiceConfig.TransportType = [Microsoft.PowerShell.EditorServices.Host.EditorServiceTransportType]::Stdio
@@ -143,23 +151,24 @@ function Compress-LogDir {
143151

144152
begin {
145153
function LegacyZipFolder($Path, $ZipPath) {
146-
if (!(Test-Path($ZipPath))) {
147-
Set-Content -LiteralPath $ZipPath -Value ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
148-
(Get-Item $ZipPath).IsReadOnly = $false
154+
if (!(Microsoft.PowerShell.Management\Test-Path($ZipPath))) {
155+
$zipMagicHeader = "PK" + [char]5 + [char]6 + ("$([char]0)" * 18)
156+
Microsoft.PowerShell.Management\Set-Content -LiteralPath $ZipPath -Value $zipMagicHeader
157+
(Microsoft.PowerShell.Management\Get-Item $ZipPath).IsReadOnly = $false
149158
}
150159

151-
$shellApplication = New-Object -ComObject Shell.Application
160+
$shellApplication = Microsoft.PowerShell.Utility\New-Object -ComObject Shell.Application
152161
$zipPackage = $shellApplication.NameSpace($ZipPath)
153162

154-
foreach ($file in (Get-ChildItem -LiteralPath $Path)) {
163+
foreach ($file in (Microsoft.PowerShell.Management\Get-ChildItem -LiteralPath $Path)) {
155164
$zipPackage.CopyHere($file.FullName)
156165
Start-Sleep -MilliSeconds 500
157166
}
158167
}
159168
}
160169

161170
end {
162-
$zipPath = ((Convert-Path $Path) -replace '(\\|/)$','') + ".zip"
171+
$zipPath = ((Microsoft.PowerShell.Management\Convert-Path $Path) -replace '(\\|/)$','') + ".zip"
163172

164173
if (Get-Command Microsoft.PowerShell.Archive\Compress-Archive) {
165174
if ($PSCmdlet.ShouldProcess($zipPath, "Create ZIP")) {
@@ -196,7 +205,7 @@ function Get-PowerShellEditorServicesVersion {
196205
$versionInfo += "macOS $(lsb_release -d -s)$nl"
197206
}
198207
else {
199-
$osInfo = Get-CimInstance Win32_OperatingSystem
208+
$osInfo = CimCmdlets\Get-CimInstance Win32_OperatingSystem
200209
$versionInfo += "Windows $($osInfo.OSArchitecture) $($osInfo.Version)$nl"
201210
}
202211

module/PowerShellEditorServices/Start-EditorServices.ps1

+9-9
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,17 @@ function ExitWithError($errorString) {
102102
}
103103

104104
function WriteSessionFile($sessionInfo) {
105-
$sessionInfoJson = ConvertTo-Json -InputObject $sessionInfo -Compress
105+
$sessionInfoJson = Microsoft.PowerShell.Utility\ConvertTo-Json -InputObject $sessionInfo -Compress
106106
Log "Writing session file with contents:"
107107
Log $sessionInfoJson
108-
$sessionInfoJson | Set-Content -Force -Path "$SessionDetailsPath" -ErrorAction Stop
108+
$sessionInfoJson | Microsoft.PowerShell.Management\Set-Content -Force -Path "$SessionDetailsPath" -ErrorAction Stop
109109
}
110110

111111
# Are we running in PowerShell 2 or earlier?
112112
if ($PSVersionTable.PSVersion.Major -le 2) {
113113
# No ConvertTo-Json on PSv2 and below, so write out the JSON manually
114114
"{`"status`": `"failed`", `"reason`": `"unsupported`", `"powerShellVersion`": `"$($PSVersionTable.PSVersion.ToString())`"}" |
115-
Set-Content -Force -Path "$SessionDetailsPath" -ErrorAction Stop
115+
Microsoft.PowerShell.Management\Set-Content -Force -Path "$SessionDetailsPath" -ErrorAction Stop
116116

117117
ExitWithError "Unsupported PowerShell version $($PSVersionTable.PSVersion), language features are disabled."
118118
}
@@ -133,9 +133,9 @@ $isPS5orLater = $PSVersionTable.PSVersion.Major -ge 5
133133

134134
# If PSReadline is present in the session, remove it so that runspace
135135
# management is easier
136-
if ((Get-Module PSReadline).Count -gt 0) {
136+
if ((Microsoft.PowerShell.Core\Get-Module PSReadline).Count -gt 0) {
137137
LogSection "Removing PSReadLine module"
138-
Remove-Module PSReadline -ErrorAction SilentlyContinue
138+
Microsoft.PowerShell.Core\Remove-Module PSReadline -ErrorAction SilentlyContinue
139139
}
140140

141141
# This variable will be assigned later to contain information about
@@ -146,7 +146,7 @@ $resultDetails = $null;
146146
function Test-ModuleAvailable($ModuleName, $ModuleVersion) {
147147
Log "Testing module availability $ModuleName $ModuleVersion"
148148

149-
$modules = Get-Module -ListAvailable $moduleName
149+
$modules = Microsoft.PowerShell.Core\Get-Module -ListAvailable $moduleName
150150
if ($modules -ne $null) {
151151
if ($ModuleVersion -ne $null) {
152152
foreach ($module in $modules) {
@@ -182,7 +182,7 @@ function Test-PortAvailability {
182182
$ipAddress = [System.Net.IPAddress]::Loopback
183183
Log "Testing availability of port ${PortNumber} at address ${ipAddress} / $($ipAddress.AddressFamily)"
184184

185-
$tcpListener = New-Object System.Net.Sockets.TcpListener @($ipAddress, $PortNumber)
185+
$tcpListener = Microsoft.PowerShell.Utility\New-Object System.Net.Sockets.TcpListener @($ipAddress, $PortNumber)
186186
$tcpListener.Start()
187187
$tcpListener.Stop()
188188
}
@@ -202,7 +202,7 @@ function Test-PortAvailability {
202202
}
203203

204204
$portsInUse = @{}
205-
$rand = New-Object System.Random
205+
$rand = Microsoft.PowerShell.Utility\New-Object System.Random
206206
function Get-AvailablePort() {
207207
$triesRemaining = 10;
208208

@@ -247,7 +247,7 @@ try {
247247
LogSection "Start up PowerShellEditorServices"
248248
Log "Importing PowerShellEditorServices"
249249

250-
Import-Module PowerShellEditorServices -ErrorAction Stop
250+
Microsoft.PowerShell.Core\Import-Module PowerShellEditorServices -ErrorAction Stop
251251

252252
# Locate available port numbers for services
253253
# There could be only one service on Stdio channel

src/PowerShellEditorServices/Session/RemoteFileManager.cs

+17-16
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function Open-EditorFile {
7575
7676
foreach ($fileName in $Paths)
7777
{
78-
dir $fileName | where { ! $_.PSIsContainer } | foreach {
78+
Microsoft.PowerShell.Management\Get-ChildItem $fileName | Where-Object { ! $_.PSIsContainer } | Foreach-Object {
7979
$filePathName = $_.FullName
8080
8181
# Get file contents
@@ -89,10 +89,10 @@ function Open-EditorFile {
8989
$params['Encoding']='Byte'
9090
}
9191
92-
$contentBytes = Get-Content @params
92+
$contentBytes = Microsoft.PowerShell.Management\Get-Content @params
9393
9494
# Notify client for file open.
95-
New-Event -SourceIdentifier PSESRemoteSessionOpenFile -EventArguments @($filePathName, $contentBytes, $preview) > $null
95+
Microsoft.PowerShell.Utility\New-Event -SourceIdentifier PSESRemoteSessionOpenFile -EventArguments @($filePathName, $contentBytes, $preview) > $null
9696
}
9797
}
9898
}
@@ -148,7 +148,7 @@ function New-EditorFile {
148148
if ($Path) {
149149
foreach ($fileName in $Path)
150150
{
151-
if (-not (Test-Path $fileName) -or $Force) {
151+
if (-not (Microsoft.PowerShell.Management\Test-Path $fileName) -or $Force) {
152152
$valueList > $fileName
153153
154154
# Get file contents
@@ -162,7 +162,7 @@ function New-EditorFile {
162162
$params['Encoding']='Byte'
163163
}
164164
165-
$contentBytes = Get-Content @params
165+
$contentBytes = Microsoft.PowerShell.Management\Get-Content @params
166166
167167
if ($Path.Count -gt 1) {
168168
$preview = $false
@@ -171,25 +171,25 @@ function New-EditorFile {
171171
}
172172
173173
# Notify client for file open.
174-
New-Event -SourceIdentifier PSESRemoteSessionOpenFile -EventArguments @($fileName, $contentBytes, $preview) > $null
174+
Microsoft.PowerShell.Utility\New-Event -SourceIdentifier PSESRemoteSessionOpenFile -EventArguments @($fileName, $contentBytes, $preview) > $null
175175
} else {
176176
$PSCmdlet.WriteError( (
177-
New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList @(
177+
Microsoft.PowerShell.Utility\New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList @(
178178
[System.Exception]'File already exists.'
179179
$Null
180180
[System.Management.Automation.ErrorCategory]::ResourceExists
181181
$fileName ) ) )
182182
}
183183
}
184184
} else {
185-
$bytes = [System.Text.Encoding]::UTF8.GetBytes(($valueList | Out-String))
186-
New-Event -SourceIdentifier PSESRemoteSessionOpenFile -EventArguments @($null, $bytes) > $null
185+
$bytes = [System.Text.Encoding]::UTF8.GetBytes(($valueList | Microsoft.PowerShell.Utility\Out-String))
186+
Microsoft.PowerShell.Utility\New-Event -SourceIdentifier PSESRemoteSessionOpenFile -EventArguments @($null, $bytes) > $null
187187
}
188188
}
189189
}
190190
191-
Set-Alias psedit Open-EditorFile -Scope Global
192-
Export-ModuleMember -Function Open-EditorFile, New-EditorFile
191+
Microsoft.PowerShell.Utility\Set-Alias psedit Open-EditorFile -Scope Global
192+
Microsoft.PowerShell.Core\Export-ModuleMember -Function Open-EditorFile, New-EditorFile
193193
";
194194

195195
// This script is templated so that the '-Forward' parameter can be added
@@ -199,14 +199,15 @@ function New-EditorFile {
199199
[string] $PSEditModule
200200
)
201201
202-
Register-EngineEvent -SourceIdentifier PSESRemoteSessionOpenFile -Forward -SupportEvent
203-
New-Module -ScriptBlock ([Scriptblock]::Create($PSEditModule)) -Name PSEdit | Import-Module -Global
202+
Microsoft.PowerShell.Utility\Register-EngineEvent -SourceIdentifier PSESRemoteSessionOpenFile -Forward -SupportEvent
203+
Microsoft.PowerShell.Core\New-Module -ScriptBlock ([Scriptblock]::Create($PSEditModule)) -Name PSEdit |
204+
Microsoft.PowerShell.Core\Import-Module -Global
204205
";
205206

206207
private const string RemovePSEditFunctionScript = @"
207-
Get-Module PSEdit | Remove-Module
208+
Microsoft.PowerShell.Core\Get-Module PSEdit | Microsoft.PowerShell.Core\Remove-Module
208209
209-
Unregister-Event -SourceIdentifier PSESRemoteSessionOpenFile -Force -ErrorAction Ignore
210+
Microsoft.PowerShell.Utility\Unregister-Event -SourceIdentifier PSESRemoteSessionOpenFile -Force -ErrorAction Ignore
210211
";
211212

212213
private const string SetRemoteContentsScript = @"
@@ -226,7 +227,7 @@ function New-EditorFile {
226227
$params['Encoding']='Byte'
227228
}
228229
229-
Set-Content @params 2>&1
230+
Microsoft.PowerShell.Management\Set-Content @params 2>&1
230231
";
231232

232233
#endregion

0 commit comments

Comments
 (0)