powershell - 有哪些不同的 PowerShell 文件类型?
问题描述
如果我尝试在 ISE 中保存文件,默认扩展名是Powershell Files
带括号(*.ps1, *.psm1, *.psd1, *.ps1xml, *.pssc, *.psrc, *.cdxml)
和描述的
Powershell Scripts (*.ps1)
Powershell Modules (*.psm1)
Powershell Data Files (*.psd1)
Powershell Session Configuration Files (*.pssc)
Powershell Role Capability Files (*.psrc)
Powershell Xml Files (*.psxml, *.cdxml)
这些都有什么作用?
解决方案
.ps1
文件是 PowerShell 脚本;它是最常见的 PowerShell 文件类型,也是最类似于其他 shell 脚本的一种,.sh
例如.bat
文件.psm1
文件是 PowerShell 模块;这些可以包含作为包交互的多个功能脚本,可以使用 cmdlet 将模块导入 PowerShellImport-Module
以及Install-Module
- 其他模块交互 cmdlet:
Export-ModuleMember
Find-Module
Get-InstalledModule
Get-Module
ImportSystemModules
New-Module
New-ModuleManifest
- 等,
*Module*
在 PowerShell 中通过选项卡
- 其他模块交互 cmdlet:
.psd1
文件是 PowerShell 数据文件或 PowerShell 模块清单文件;它们提供信息和说明,并用作psm1
文件/PowerShell 模块的清单文件;存储配置数据。可以作为 PowerShell 模块的帮助文件。有关详细信息,请参阅https://docs.microsoft.com/en-us/powershell/scripting/developer/module/how-to-write-a-powershell-module-manifest?view=powershell-7。使用Import-PowerShellDataFile
、New-ModuleManifest
、Test-ModuleManifest
、Update-ModuleManifest
和Import-LocalizedData
cmdlet 进行操作。.ps1xml
files 是XML
PowerShell 模块的 PowerShell,类似于.psd1
files,但采用 XML 格式。支持可更新帮助功能的模块也需要模块清单。可更新帮助使用模块清单中的 HelpInfoUri 键来查找包含模块更新帮助文件位置的帮助信息 (HelpInfo XML) 文件。PowerShell XML 文件可以用作模块的帮助文件:需要存在模块清单文件才能执行此操作。ps1xml
指定用于执行某些操作的文件有某些名称,请参阅https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_types.ps1xml?view=powershell-7和https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_format.ps1xml?view=powershell-7.pssc
文件是 PowerShell 会话配置文件;描述会话配置文件,这些文件在会话配置(也称为“端点”)中用于定义使用会话配置的会话环境。会话配置文件可以轻松创建自定义会话配置,而无需使用复杂的 C# 程序集或脚本。.pssc
文件采用哈希表格式并包含 PowerShell 会话的配置信息。请参阅https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_session_configuration_files?view=powershell-7。.pssc
文件使用Register-PSSessionConfiguration
、Set-PSSessionConfiguration
和New-PSSessionConfigurationFile
cmdlet。.psrc
文件是 PowerShell 角色能力文件;它们定义了一组在会话配置和会话配置 (pssc
) 文件中使用的功能。使用哈希表格式。使用New-PSRoleCapabilityFile
cmdlet。请参阅https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/new-psrolecapabilityfile?view=powershell-7.cdxml
文件是使用 CIM 工具创建 cmdlet 的 PowerShell XML 文件。请参阅https://docs.microsoft.com/en-us/previous-versions/windows/desktop/wmi_v2/cdxml-overview
例子:
示例psd1
:
@{
RootModule = 'ExampleModule.psm1'
ModuleVersion = '0.0.1'
GUID = '1234abcd-1234-abcd-xxxx-000000000000'
Author = 'User01'
CompanyName = 'Unknown'
Copyright = '(c) User01. All rights reserved.'
Description = 'Example module'
示例types.ps1xml
:
<Type>
<Name>System.Object[]</Name>
<Members>
<AliasProperty>
<Name>Test</Name>
<ReferencedMemberName>
Length
</ReferencedMemberName>
</AliasProperty>
</Members>
</Type>
使用成员类型将新成员添加Count
到s :System.Array
AliasProperty
gm -InputObject @(1,2,3,4)| ?{$_.Name -eq "test"}
TypeName: System.Object[]
Name MemberType Definition
---- ---------- ----------
Test AliasProperty Test = Length
示例psrc
:
New-PSRoleCapabilityFile `
-Path ".\Test.psrc" `
-Author "TestUser" `
-CompanyName "Test Company" `
-Description "Description" `
-ModulesToImport "Microsoft.PowerShell.Core"
Test.psrc
使用参数
Test.psrc
内容创建:
@{
# ID used to uniquely identify this document
GUID = '4fb9608e-474c-44c3-a752-ea1d9b27dcb0'
# Author of this document
Author = 'TestUser'
# Description of the functionality provided by these settings
Description = 'Description'
# Company associated with this document
CompanyName = 'Test Company'
# Copyright statement for this document
Copyright = '(c) 2020 TestUser. All rights reserved.'
# Modules to import when applied to a session
ModulesToImport = 'Microsoft.PowerShell.Core'
# Aliases to make visible when applied to a session
# VisibleAliases = 'Item1', 'Item2'
# Cmdlets to make visible when applied to a session
# VisibleCmdlets = 'Invoke-Cmdlet1', @{ Name = 'Invoke-Cmdlet2'; Parameters = @{ Name = 'Parameter1'; ValidateSet = 'Item1', 'Item2' }, @{ Name = 'Parameter2'; ValidatePattern = 'L*' } }
# Functions to make visible when applied to a session
# VisibleFunctions = 'Invoke-Function1', @{ Name = 'Invoke-Function2'; Parameters = @{ Name = 'Parameter1'; ValidateSet = 'Item1', 'Item2' }, @{ Name = 'Parameter2'; ValidatePattern = 'L*' } }
# External commands (scripts and applications) to make visible when applied to a session
# VisibleExternalCommands = 'Item1', 'Item2'
# Providers to make visible when applied to a session
# VisibleProviders = 'Item1', 'Item2'
# Scripts to run when applied to a session
# ScriptsToProcess = 'C:\ConfigData\InitScript1.ps1', 'C:\ConfigData\InitScript2.ps1'
# Aliases to be defined when applied to a session
# AliasDefinitions = @{ Name = 'Alias1'; Value = 'Invoke-Alias1'}, @{ Name = 'Alias2'; Value = 'Invoke-Alias2'}
# Functions to define when applied to a session
# FunctionDefinitions = @{ Name = 'MyFunction'; ScriptBlock = { param($MyInput) $MyInput } }
# Variables to define when applied to a session
# VariableDefinitions = @{ Name = 'Variable1'; Value = { 'Dynamic' + 'InitialValue' } }, @{ Name = 'Variable2'; Value = 'StaticInitialValue' }
# Environment variables to define when applied to a session
# EnvironmentVariables = @{ Variable1 = 'Value1'; Variable2 = 'Value2' }
# Format files (.ps1xml) to load when applied to a session
# FormatsToProcess = 'C:\ConfigData\MyFormats.ps1xml', 'C:\ConfigData\OtherFormats.ps1xml'
# Assemblies to load when applied to a session
# AssembliesToLoad = 'System.Web', 'System.OtherAssembly, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
}
例子pssc
New-PSSessionConfigurationFile -RoleDefinitions
@{ 'User' = @{ RoleCapabilities = 'Maintenance' }}
-Path .\Maintenance.pssc
Maintenance.pssc
用内容创作
@{
# Version number of the schema used for this document
SchemaVersion = '2.0.0.0'
# ID used to uniquely identify this document
GUID = 'a4f7e8eb-f787-4f2c-bf0f-555db2ca2b05'
# Author of this document
Author = 'User'
# Description of the functionality provided by these settings
# Description = ''
# Session type defaults to apply for this session configuration. Can be 'RestrictedRemoteServer' (recommended), 'Empty', or 'Default'
SessionType = 'Default'
# Directory to place session transcripts for this session configuration
# TranscriptDirectory = 'C:\Transcripts\'
# Whether to run this session configuration as the machine's (virtual) administrator account
# RunAsVirtualAccount = $true
# Scripts to run when applied to a session
# ScriptsToProcess = 'C:\ConfigData\InitScript1.ps1', 'C:\ConfigData\InitScript2.ps1'
# User roles (security groups), and the role capabilities that should be applied to them when applied to a session
RoleDefinitions = @{
'User' = @{
'RoleCapabilities' = 'Maintenance' } }
}
注册文件
Register-PSSessionConfiguration -Name Name -Path .\Maintenance.pssc
现在 PowerShell 会话从Name
use开始Maintenance.pssc
。Name
使用 use启动 PowerShell 会话
New-PSSession -ConfigurationName Name
推荐阅读
- javascript - 在 Svelte 中初始化 dataTable Jquery
- c# - 打开可执行文件无法按预期工作
- perl - 将 perl 中的字符串截断为带有尾随省略号的子字符串
- r - 如何使用 R 中的最大公因数创建一个函数来查找整数向量的最小公倍数?
- node.js - 在快速应用程序中查找未使用的路线或代码以删除死代码
- python - 使用python解读图像
- php - 如何用php在Jpgraph中设置虚线
- ansible - Ansible set_fact 正在覆盖项目
- python-3.x - 由于 tex 错误“RuntimeError:无法使用 tex 处理字符串,因为找不到 dvipng”而无法并行注释文本
- javascript - 删除条形图 ZingChart 上的空间