有哪些不同的 PowerShell 文件类型? [英] What are the different PowerShell file types?

查看:68
本文介绍了有哪些不同的 PowerShell 文件类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试在 ISE 中保存文件,默认扩展名是 Powershell Files 带括号 (*.ps1, *.psm1, *.psd1, *.ps1xml, *.pssc, *.psrc, *.cdxml) 和说明

If I try to save a file in ISE, the default extension is Powershell Files with parenthesis (*.ps1, *.psm1, *.psd1, *.ps1xml, *.pssc, *.psrc, *.cdxml) and with descriptions

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.bat 文件等其他 shell 脚本的一种文件

    • .ps1 files are PowerShell scripts; it is the most common type of PowerShell file and one that is the most like other shell scripts like .sh, .bat files

      .psm1 文件是 PowerShell 模块;这些可以包含作为包交互的多个函数脚本,可以使用 Import-Module cmdlet 以及 Install-Module

      .psm1 files are PowerShell modules; these can contain multiple function scripts interacted as a package, can import modules into PowerShell with the Import-Module cmdlet as well as Install-Module

      • 其他模块交互 cmdlet:
        • 导出模块成员
        • 查找模块
        • Get-InstalledModule
        • 获取模块
        • 导入系统模块
        • 新模块
        • New-ModuleManifest
        • 等,在 PowerShell 中通过 *Module* 选项卡

        .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-PowerShellDataFileNew-ModuleManifestTest-ModuleManifestUpdate-ModuleManifest 进行操作导入本地化数据 cmdlet.

        .psd1 files are PowerShell data files or PowerShell module manifest files; they provide information and instructions and serve as manifest files for psm1 files/PowerShell modules; stores configuration data. Can serve as help file for PowerShell modules. For more information, see https://docs.microsoft.com/en-us/powershell/scripting/developer/module/how-to-write-a-powershell-module-manifest?view=powershell-7. Manipulated with Import-PowerShellDataFile, New-ModuleManifest, Test-ModuleManifest, Update-ModuleManifest, and Import-LocalizedData cmdlets.

        .ps1xml 文件是 PowerShell 模块的 PowerShell XML,类似于 .psd1 文件,但采用 XML 格式.支持可更新帮助功能的模块也需要模块清单.可更新帮助使用模块清单中的 HelpInfoUri 键来查找包含模块更新帮助文件位置的帮助信息 (HelpInfo XML) 文件. PowerShell XML 文件可以用作模块的帮助文件:需要存在模块清单文件才能执行此操作.指定用于执行某些操作的 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

        .ps1xml files are PowerShell XMLs for PowerShell modules, similar to .psd1 files but in XML format. A module manifest is also required for modules that support the Updatable Help feature. Updatable Help uses the HelpInfoUri key in the module manifest to find the Help information (HelpInfo XML) file that contains the location of the updated help files for the module. PowerShell XML files can serve as the help file for a module: Requires the existence of a module manifest file to do this. There are certain names for ps1xml files that are designated to do certain things, see https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_types.ps1xml?view=powershell-7 and 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-PSSessionConfigurationSet-PSSessionConfigurationNew-PSSessionConfigurationFile cmdlet.

        .pssc files are PowerShell session configuration files; Describes session configuration files, which are used in a session configuration (also known as an "endpoint") to define the environment of sessions that use the session configuration. Session configuration files make it easy to create custom session configurations without using complex C# assemblies or scripts. .pssc files are in hashtable format and contain the configuration information for a PowerShell session. See https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_session_configuration_files?view=powershell-7. .pssc files use the Register-PSSessionConfiguration, Set-PSSessionConfiguration, and New-PSSessionConfigurationFile cmdlets.

        .psrc 文件是 PowerShell 角色能力文件;它们定义了一组在会话配置和会话配置 (pssc) 文件中使用的功能.使用哈希表格式.使用 New-PSRoleCapabilityFile cmdlet.请参阅 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/new-psrolecapabilityfile?view=powershell-7

        .psrc files are PowerShell role capability files; they define a set of capabilities that are used in session configuration and session configuration (pssc) files. Uses hashtable format. Uses the New-PSRoleCapabilityFile cmdlet. See 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

        .cdxml files are PowerShell XML files that create cmdlets using CIM tools. See 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 添加到具有成员类型 AliasPropertySystem.Array :

        Adds new member Count to System.Arrays with member type 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
        

        现在以 Name 开始的 PowerShell 会话使用 Maintenance.pssc.要使用 Name 启动 PowerShell 会话,请使用

        Now PowerShell sessions started with Name use Maintenance.pssc. To start a PowerShell session with Name use

        New-PSSession -ConfigurationName Name
        

        这篇关于有哪些不同的 PowerShell 文件类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆