首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不同的PowerShell文件类型是什么?

不同的PowerShell文件类型是什么?
EN

Stack Overflow用户
提问于 2020-06-27 00:36:32
回答 1查看 6.1K关注 0票数 8

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

代码语言:javascript
运行
复制
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)

这些都是做什么的?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-27 02:11:34

  • .ps1文件是PowerShell脚本;它是最常见的PowerShell文件类型,也是最像.sh.bat文件和

等其他shell脚本的文件。

  • .psm1文件是PowerShell模块;它们可以包含作为一个包交互的多个函数脚本,可以通过Import-Module cmdlet和Install-Module

将模块导入PowerShell。

代码语言:javascript
运行
复制
- Other Module-Interaction cmdlets: 
    - `Export-ModuleMember`
    - `Find-Module`
    - `Get-InstalledModule`
    - `Get-Module`
    - `ImportSystemModules`
    - `New-Module`
    - `New-ModuleManifest`
    - etc, tab through `*Module*` in PowerShell

操作

示例:

示例psd1

代码语言:javascript
运行
复制
@{

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

代码语言:javascript
运行
复制
<Type>
  <Name>System.Object[]</Name>
  <Members>
    <AliasProperty>
      <Name>Test</Name>
      <ReferencedMemberName>
        Length
      </ReferencedMemberName>
    </AliasProperty>
  </Members>
</Type>

使用成员类型Count将新成员AliasProperty添加到System.Array

代码语言:javascript
运行
复制
gm -InputObject @(1,2,3,4)| ?{$_.Name -eq "test"}


   TypeName: System.Object[]

Name       MemberType    Definition
----       ----------    ----------
Test       AliasProperty Test = Length

示例psrc

代码语言:javascript
运行
复制
New-PSRoleCapabilityFile `
    -Path ".\Test.psrc" `
    -Author "TestUser" `
    -CompanyName "Test Company" `
    -Description "Description" `
    -ModulesToImport "Microsoft.PowerShell.Core"

使用参数创建Test.psrc

Test.psrc内容:

代码语言:javascript
运行
复制
@{

# 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

代码语言:javascript
运行
复制
New-PSSessionConfigurationFile -RoleDefinitions
@{ 'User' = @{ RoleCapabilities = 'Maintenance' }}
-Path .\Maintenance.pssc

创建具有内容的Maintenance.pssc

代码语言:javascript
运行
复制
@{

# 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' } }

}

注册文件

代码语言:javascript
运行
复制
Register-PSSessionConfiguration -Name Name -Path .\Maintenance.pssc

现在,PowerShell会话从Name use Maintenance.pssc开始。使用PowerShell启动Name会话

代码语言:javascript
运行
复制
New-PSSession -ConfigurationName Name
票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62604621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档