首页 > 解决方案 > 带有 PowerShell 7 的 Azure DSC

问题描述

我正在尝试在 Azure 中自动化 DataGateway,最近在 powershell 7 中发布了库来管理它。但是 Azure DSC 与 Windows Powershell 5 一起使用的问题,即使 VM 已经安装了 powershell 7,它也无法正常工作。

有人可以建议一个正确的方法吗?

标签: azuredscpowershell-corepowershell-7.0data-gateway

解决方案


启用执行策略:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force

安装最新版本的 NuGet 和 PowerShellGet

Install-PackageProvider Nuget -MinimumVersion 2.8.5.201 -Force | Out-Null
Install-Module -Name PowerShellGet -Force -AllowClobber 

创建 Foo 文件夹

$LFHT = @ {
ItemType = 'Directory'
ErrorAction = 'SilentlyContinue'}


New - Item - Path C: \Foo@ LFHT | Out - Null

这些命令使用称为 splatting 的 PowerShell 技术。有关更多信息,您可以键入:

 Get-Help about _ splattin

下载 PowerShell 7 安装脚本

Set-Location C:\Foo
$URI = "https://aka.ms/install-powershell.ps1"
Invoke-RestMethod -Uri $URI | 
Out-File -FilePath C:\Foo\Install-PowerShell.ps1

查看安装文件帮助信息

Get-Help -Name C:\Foo\Install-PowerShell.ps1

安装 PowerShell 7

$EXTHT = @ {
UseMSI = $true
Quiet = $true
AddExplorerContextMenu = $true
EnablePSRemoting = $true}


C:\Foo\Install-PowerShell.ps1 @EXTHT

下载 PowerShell 7 MSI 安装包的安装脚本然后静默运行此代码。

为调出 PowerShell 7 控制台而运行的可执行程序的名称是 pwsh.exe。

另一个明显的区别是 PowerShell 7 没有 .PS1XML 文件。

检查安装文件夹

Get-Childitem -Path $env:ProgramFiles\PowerShell\7 -Recurse |
Measure-Object -Property Length -Sum

查看模块文件夹位置

 $I = 0
$env:PSModulePath -split ';' |  
Foreach-Object { 
"[{0:N0}]   {1}" -f $I++, $_}

查看配置文件位置:

ISE内部

$PROFILE |
Format-List -Property *Host* -Force

从 Windows PowerShell 控制台

powershell -Command '$Profile|   Format-List -Property *Host*' -Force

启动 PowerShell 7

打开 pwsh.exe,然后按 Enter 打开 PowerShell 7 控制台。打开 PowerShell 7 控制台后,通过查看 $PSVersionTable 变量来验证版本

$PSVersionTable

查看模块文件夹的新位置

$ModFolders = $Env:PSModulePath -split ';'
$I = 0$
ModFolders | 
ForEach-Object {"[{0:N0}]   {1}" -f $I++, $_}

查看配置文件的新位置

$PROFILE | Format-List -Property *Host* -Force

推荐阅读