gpt4 book ai didi

powershell - 有哪些不同的 PowerShell 文件类型?

转载 作者:行者123 更新时间:2023-12-02 22:18:48 27 4
gpt4 key购买 nike

如果我尝试在 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 文件类型,也是最像 .sh 等其他 shell 脚本的类型。 , .bat文件
  • .psm1文件是 PowerShell 模块;这些可以包含作为一个包交互的多个功能脚本,可以使用 Import-Module 将模块导入 PowerShell cmdlet 以及 Install-Module
  • 其他模块交互 cmdlet:
  • Export-ModuleMember
  • Find-Module
  • Get-InstalledModule
  • Get-Module
  • ImportSystemModules
  • New-Module
  • New-ModuleManifest
  • 等,通过 *Module* 制表符在 PowerShell


  • .psd1文件是 PowerShell 数据文件或 PowerShell 模块 list 文件;它们提供信息和说明,并用作 psm1 的 list 文件文件/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文件是 PowerShell XML s 用于 PowerShell 模块,类似于 .psd1文件,但采用 XML 格式。支持可更新帮助功能的模块也需要模块 list 。可更新帮助使用模块 list 中的 HelpInfoUri 键来查找包含模块更新帮助文件位置的帮助信息 (HelpInfo XML) 文件。 PowerShell XML 文件可以用作模块的帮助文件:需要存在模块 list 文件才能执行此操作。 ps1xml 有一些名称指定用于执行某些操作的文件,请参阅 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_types.ps1xml?view=powershell-7https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_format.ps1xml?view=powershell-7
  • .pssc文件是 PowerShell session 配置文件;描述 session 配置文件,这些文件在 session 配置(也称为“端点”)中用于定义使用 session 配置的 session 环境。 session 配置文件可以轻松创建自定义 session 配置,而无需使用复杂的 C# 程序集或脚本。 .pssc文件采用哈希表格式并包含 PowerShell session 的配置信息。见 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 角色能力文件;它们定义了一组在 session 配置和 session 配置 (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>
    添加新成员 CountSystem.Array s 成员类型为 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 session 以 Name 开始使用 Maintenance.pssc .使用 Name 启动 PowerShell session 采用
    New-PSSession -ConfigurationName Name

    关于powershell - 有哪些不同的 PowerShell 文件类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62604621/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com