Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell .ps1脚本文件中的命令或模块依赖项列表_Powershell_Script - Fatal编程技术网

Powershell .ps1脚本文件中的命令或模块依赖项列表

Powershell .ps1脚本文件中的命令或模块依赖项列表,powershell,script,Powershell,Script,是否有某种方法可以标识脚本文件中使用的所有cmdlet?我想了解的是,如何找到部署到另一个环境的需求。使用了哪些模块?安装了哪个版本?然后可以查看命令列表并知道在新环境中安装什么 让我们以下面的文件为例。了解cmdlet后,我可以使用get命令查找已安装的模块/版本。参考: 谢谢你的建议 Cmdlet依赖项(示例输出) Cmdlet信息(示例输出) PbiList_Reports_exportCsv.ps1(源输入) Account>Container>Blob #---------------

是否有某种方法可以标识脚本文件中使用的所有cmdlet?我想了解的是,如何找到部署到另一个环境的需求。使用了哪些模块?安装了哪个版本?然后可以查看命令列表并知道在新环境中安装什么

让我们以下面的文件为例。了解cmdlet后,我可以使用get命令查找已安装的模块/版本。参考:

谢谢你的建议

Cmdlet依赖项(示例输出) Cmdlet信息(示例输出) PbiList_Reports_exportCsv.ps1(源输入)
Account>Container>Blob
#------------------------------------------------------#>
#-----导出文件本地目录--------
$Filepath=“C:\Users\me\Dropbox\00-PBI\ActivityLogs\”
$Transcript\u file=$Filepath+“Logs\testlog\u reports.txt”
$Reports\u file=$Filepath+“AllReports.csv”
###$wsReportsAll |导出csv-路径$Reports_文件-NoTypeInformation
#
#-----将文件导出到Azure blob--------
$Transcript\u file=“testfun\u reports.txt”
$Reports\u file=“AllReports.csv”
#获取存储帐户的密钥
#映射到reports BLOB上下文
#将文件复制到存储帐户
$acctKey=(Get-AzStorageAccountKey-名称“myblob”-ResourceGroupName“myResource”)。值[0]
$storageContext=New-AzStorageContext-StorageAccountName“myblob”-StorageAccountKey$acctKey-Verbose
####将主机打印写入日志文件
启动脚本-路径“$Env:temp/$Transcript_file”-verbose#“C:\Users\me\Dropbox\00-PBI\ActivityLogs\Logs\testlog\u reports.txt”
编写主机“今天:($Date.ToString(“yyyy.MM.dd”)”
#-----PBI连接--------
写入主机“PBI凭据…”-ForegroundColor黄色-BackgroundColor深绿色
##PBI凭证
$password=“*******”|转换为SecureString-asPlainText-Force
$username=”me@company.com" 
$credential=新对象System.Management.Automation.PSCredential($username,$password)
连接PowerBIServiceAccount-凭据$Credential
#--------PBI工作区报告-------
$wsReportsAll=@()
#已筛选工作空间以排除PBI个人组和其他PBI组
写入主机“工作区信息…”-ForegroundColor黄色-BackgroundColor深绿色
$Groups=Get PowerBIWorkspace-All-Scope组织| SORT@{Expression=“Type”;Descending=$True},Name |
?{
($\类型-eq“工作区”)`
}
写入主机(($Groups.count).ToString()+“工作区计数”)
foreach($ws在$Groups中){
#变数
$wsName=$ws.Name.ToString()
$wsId=$ws.Id.ToString()
#工作区
$wsReports=Get PowerBIReport-WorkspaceId$ws.Id
$wsReports=$wsReports | SELECT*,@{Name='Workspace';Expression={$wsName}},@{Name='WorkspaceId';Expression={$wsId}
写入主机(“”)
写入主机($ws.Name+“…”)
写入主机($wsReports.count).ToString()+“报告计数”)-ForegroundColor青色-BackgroundColor深绿色
$wsReportsAll+=$wsReports
}
$wsReportsAll |选择Id、名称、工作空间Id、工作空间、数据集Id | ft-自动
$wsReportsCsv=($wsReportsAll | ConvertTo Csv-NoTypeInformation).ToString()
$wsReportsAll |导出csv“$Env:temp/$Reports_文件”
#$wsReportsAll |导出csv-路径$Reports_文件-NoTypeInformation
设置AzStorageBlobContent-文件“$Env:temp/$Reports_File”-容器“myFolder”-BlobType“Block”-上下文$storageContext-强制-详细
停止记录
设置AzStorageBlobContent-文件“$Env:temp/$Transcript_File”-容器“myFolder”-Blob“Logs/$Transcript_File”-BlobType“Block”-上下文$storageContext-强制-详细
#****************
#****************
首先,(可选)从中应用
调用ScriptAnalyzer
,以修复较小的不一致性(特别是,应用
PSAVIdUsingCmdletAlias
规则)

然后您可以使用,例如,如下所示:

# Define input script
$inputScript = '.\tests\PbiList_Reports_exportCsv.ps1'

("-" * 25) + ' ScriptAnalyzer'
Import-Module -Name PSScriptAnalyzer
Invoke-ScriptAnalyzer -Path "$inputScript" -ExcludeRule PSAvoidUsingWriteHost -Fix

# Empty collection for errors
$Errors = [System.Management.Automation.Language.ParseError[]]@()
# Empty collection for errors
$Tokens = [System.Management.Automation.Language.Token[]]@()

$Parsed = [System.Management.Automation.Language.Parser]::ParseFile(
                $inputScript,
                [ref]$Tokens,
                [ref]$Errors)

("=" * 25) + " Errors"
if ($Errors.Count -gt 0 ) {
    Write-Warning "$($Errors.Count) Errors found"
}

("≡" * 25) + " Commands"
$Commands = $tokens |
    Where-Object TokenFlags -eq "CommandName" |
        Sort-Object -Property Value -Unique
$Commands | Select-Object -ExpandProperty Value
### I can't do the following not having installed all modules
### $Commands | ForEach-Object { Get-Command -Name $_.Value }
输出
\SO\67519696.ps1

------------------------- ScriptAnalyzer

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSAvoidUsingConvertToSecureStringWi Error        PbiList_Re 39    File
thPlainText                                      ports_expo       'PbiList_Reports_exportCsv.ps1'
                                                 rtCsv.ps1        uses ConvertTo-SecureString with
                                                                  plaintext. This will expose secure
                                                                  information. Encrypted standard
                                                                  strings should be used instead.
PSUseDeclaredVarsMoreThanAssignment Warning      PbiList_Re 74    The variable 'wsReportsCsv' is
s                                                ports_expo       assigned but never used.
                                                 rtCsv.ps1
========================= Errors
≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡ Commands
Connect-PowerBIServiceAccount
ConvertTo-Csv
ConvertTo-SecureString
Export-csv
Format-Table
Get-AzStorageAccountKey
Get-PowerBIReport
Get-PowerBIWorkspace
New-AzStorageContext
New-Object
Select-Object
Set-AzStorageBlobContent
Sort-Object
Start-Transcript
Stop-Transcript
Where-Object
Write-Host

它工作得很好,你是上帝派来的。我怎么能竖起+10个大拇指?非常感谢。
<#------------------------------------------------------
# 4,REPORT: Export list of all workspace reports 
# 
# Loop all workspaces. Retreive reports list. Export to blob storage.
# --> Account > Container > Blob
#------------------------------------------------------#>

# -------- Export files local dir --------
$Filepath = "C:\Users\me\Dropbox\00-PBI\ActivityLogs\"
$Transcript_file = $Filepath +"Logs\testlog_reports.txt"
$Reports_file = $Filepath +"AllReports.csv"
### $wsReportsAll | Export-csv -Path $Reports_file -NoTypeInformation
#

# -------- Export files to Azure blob --------
$Transcript_file = "testfun_reports.txt"
$Reports_file = "AllReports.csv"

# Get key to storage account
# Map to the reports BLOB context
# Copy the file to the storage account
$acctKey = (Get-AzStorageAccountKey -Name "myblob" -ResourceGroupName "myResource").Value[0] 
$storageContext = New-AzStorageContext -StorageAccountName "myblob" -StorageAccountKey $acctKey -Verbose


#### Print write-host to log file
Start-Transcript -Path "$Env:temp/$Transcript_file" -verbose # "C:\Users\me\Dropbox\00-PBI\ActivityLogs\Logs\testlog_reports.txt"
Write-Host "Today: ($Date).ToString("yyyy.MM.dd")" 


# -------- PBI Connection --------

Write-Host " PBI credentials ..." -ForegroundColor Yellow -BackgroundColor DarkGreen
## PBI credentials 
$password = "******" | ConvertTo-SecureString -asPlainText -Force
$username = "me@company.com" 
$credential = New-Object System.Management.Automation.PSCredential($username, $password)
Connect-PowerBIServiceAccount -Credential $credential


#-------- PBI Workspace Reports  -------
$wsReportsAll = @()

# Workspaces filtered to exclude PBI Personal and other PBI Groups
Write-Host " Workspace info ..." -ForegroundColor Yellow -BackgroundColor DarkGreen
$Groups = Get-PowerBIWorkspace -All -Scope Organization | SORT @{Expression="Type"; Descending=$True}, Name | 
    ?{
    ($_.Type -eq 'Workspace') `
    }

Write-Host(($Groups.count).ToString() + " workspace count")

foreach ($ws in $Groups) {
    # Variables
    $wsName = $ws.Name.ToString()
    $wsId = $ws.Id.ToString()
    
    # Workspaces
    $wsReports = Get-PowerBIReport -WorkspaceId $ws.Id
    $wsReports = $wsReports | SELECT *, @{Name = 'Workspace'; Expression = {$wsName}} , @{Name = 'WorkspaceId'; Expression = {$wsId}} 
    
    Write-Host("")
    Write-Host($ws.Name + "...")
    Write-Host(($wsReports.count).ToString() + " report count") -ForegroundColor Cyan -BackgroundColor DarkGreen

    $wsReportsAll += $wsReports
}

$wsReportsAll | SELECT Id, Name, WorkspaceId, Workspace, DatasetId  | ft -auto
$wsReportsCsv = ($wsReportsAll | ConvertTo-Csv -NoTypeInformation).ToString() 
$wsReportsAll | Export-csv "$Env:temp/$Reports_file"

# $wsReportsAll | Export-csv -Path $Reports_file -NoTypeInformation
Set-AzStorageBlobContent -File "$Env:temp/$Reports_file" -Container "myFolder" -BlobType "Block" -Context $storageContext -Force -Verbose

Stop-Transcript

Set-AzStorageBlobContent -File "$Env:temp/$Transcript_file" -Container "myFolder" -Blob "Logs/$Transcript_file" -BlobType "Block" -Context $storageContext -Force -Verbose

#****************
#****************
# Define input script
$inputScript = '.\tests\PbiList_Reports_exportCsv.ps1'

("-" * 25) + ' ScriptAnalyzer'
Import-Module -Name PSScriptAnalyzer
Invoke-ScriptAnalyzer -Path "$inputScript" -ExcludeRule PSAvoidUsingWriteHost -Fix

# Empty collection for errors
$Errors = [System.Management.Automation.Language.ParseError[]]@()
# Empty collection for errors
$Tokens = [System.Management.Automation.Language.Token[]]@()

$Parsed = [System.Management.Automation.Language.Parser]::ParseFile(
                $inputScript,
                [ref]$Tokens,
                [ref]$Errors)

("=" * 25) + " Errors"
if ($Errors.Count -gt 0 ) {
    Write-Warning "$($Errors.Count) Errors found"
}

("≡" * 25) + " Commands"
$Commands = $tokens |
    Where-Object TokenFlags -eq "CommandName" |
        Sort-Object -Property Value -Unique
$Commands | Select-Object -ExpandProperty Value
### I can't do the following not having installed all modules
### $Commands | ForEach-Object { Get-Command -Name $_.Value }
------------------------- ScriptAnalyzer

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSAvoidUsingConvertToSecureStringWi Error        PbiList_Re 39    File
thPlainText                                      ports_expo       'PbiList_Reports_exportCsv.ps1'
                                                 rtCsv.ps1        uses ConvertTo-SecureString with
                                                                  plaintext. This will expose secure
                                                                  information. Encrypted standard
                                                                  strings should be used instead.
PSUseDeclaredVarsMoreThanAssignment Warning      PbiList_Re 74    The variable 'wsReportsCsv' is
s                                                ports_expo       assigned but never used.
                                                 rtCsv.ps1
========================= Errors
≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡ Commands
Connect-PowerBIServiceAccount
ConvertTo-Csv
ConvertTo-SecureString
Export-csv
Format-Table
Get-AzStorageAccountKey
Get-PowerBIReport
Get-PowerBIWorkspace
New-AzStorageContext
New-Object
Select-Object
Set-AzStorageBlobContent
Sort-Object
Start-Transcript
Stop-Transcript
Where-Object
Write-Host