从 Configuration Manager 控制台创建并运行 PowerShell 脚本Create and run PowerShell scripts from the Configuration Manager console

适用范围: Configuration Manager (Current Branch)Applies to: Configuration Manager (current branch)

Configuration Manager 具有运行 PowerShell 脚本的集成功能。Configuration Manager has an integrated ability to run PowerShell scripts. PowerShell 的优势是创建复杂而易懂的自动执行脚本,并能与大型社区分享。PowerShell has the benefit of creating sophisticated, automated scripts that are understood and shared with a larger community. 脚本简化了自定义工具生成,便于软件管理,并让你快速完成常见任务,能够更轻松、更一致地完成大型工作。The scripts simplify building custom tools to administer software and let you accomplish mundane tasks quickly, allowing you to get large jobs done more easily and more consistently.

备注

在版本 2006 及更早版本中,Configuration Manager 默认不启用此项可选功能。In version 2006 and earlier, Configuration Manager doesn't enable this optional feature by default. 必须在使用前启用此功能。You must enable this feature before using it. 有关详细信息,请参阅启用更新中的可选功能For more information, see Enable optional features from updates.

将此集成到 Configuration Manager,可以使用“运行脚本” 功能执行以下操作:With this integration in Configuration Manager, you can use the Run Scripts functionality to do the following things:

  • 创建并编辑用于 Configuration Manager 的脚本。Create and edit scripts for use with Configuration Manager.
  • 通过角色和安全作用域管理脚本使用。Manage script usage through roles and security scopes.
  • 在集合或独立的本地托管 Windows 电脑上运行脚本。Run scripts on collections or individual on-premises managed Windows PCs.
  • 从客户端设备获取快速聚合的脚本结果。Get rapid aggregated script results from client devices.
  • 监视脚本执行,并查看脚本输出中生成的报告。Monitor script execution and view reporting results from script output.

警告

  • 脚本有如此强大的功能,记得在使用它们时仔细小心。Given the power of scripts, we remind you to be intentional and careful with their usage. 我们内置了额外的保护措施来帮助你;分离的角色和作用域。We have built in additional safeguards to assist you; segregated roles and scopes. 运行脚本之前请务必验证脚本的准确性,并确保它们来自受信任的源,以防发生意外的脚本执行操作。Be sure to validate the accuracy of scripts before running them and confirm they are from a trusted source, to prevent unintended script execution. 请注意扩展字符或其他混淆,并了解一下保护脚本的相关信息。Be mindful of extended characters or other obfuscation and educate yourself about securing scripts. 详细了解 PowerShell 脚本安全性Learn more about PowerShell script security
  • 某些反恶意软件可能会无意中触发针对 Configuration Manager 运行脚本或 CMPivot 功能的事件。Certain anti-malware software may inadvertently trigger events against the Configuration Manager Run Scripts or CMPivot features. 建议排除 %windir%\CCM\ScriptStore,以便反恶意软件允许这些功能在不受干扰的情况下运行。It is recommended to exclude %windir%\CCM\ScriptStore so that the anti-malware software permits those features to run without interference.

必备条件Prerequisites

  • 要运行 PowerShell 脚本,客户端必须运行 PowerShell 3.0 或更高版本。To run PowerShell scripts, the client must be running PowerShell version 3.0 or later. 但是,如果运行的脚本包含 PowerShell 较高版本的功能,则运行该脚本的客户端必须运行该版本的 PowerShell。However, if a script you run contains functionality from a later version of PowerShell, the client on which you run the script must be running that version of PowerShell.
  • Configuration Manager 客户端必须从版本 1706 运行客户端,否则稍后才能运行脚本。Configuration Manager clients must be running the client from the 1706 release, or later in order to run scripts.
  • 要使用这些脚本,你必须是相应 Configuration Manager 安全角色的成员。To use scripts, you must be a member of the appropriate Configuration Manager security role.
  • 导入并编写脚本 - 帐户必须对“SMS 脚本”具有“创建”权限 。To import and author scripts - Your account must have Create permissions for SMS Scripts.
  • 批准或拒绝脚本 - 帐户必须对“SMS 脚本”具有“批准”权限 。To approve or deny scripts - Your account must have Approve permissions for SMS Scripts.
  • 运行脚本 - 帐户必须对“集合”具有“运行脚本”权限 。To run scripts - Your account must have Run Script permissions for Collections.

有关 Configuration Manager 安全角色的详细信息,请参阅:For more information about Configuration Manager security roles:
运行脚本的安全作用域Security scopes for run scripts
运行脚本的安全角色Security roles for run scripts
基于角色的管理基础Fundamentals of role-based administration.

限制Limitations

“运行脚本”目前支持:Run Scripts currently supports:

  • 脚本语言:PowerShellScripting languages: PowerShell
  • 参数类型:整数、字符串和列表。Parameter types: integer, string, and list.

警告

请注意,使用参数时会打开外围应用,可能存在 PowerShell 注入攻击风险。Be aware that when using parameters, it opens a surface area for potential PowerShell injection attack risk. 可通过多种方法缓解和解决此问题,例如,使用正则表达式验证参数输入或使用预定义参数。There are various ways to mitigate and work around, such as using regular expressions to validate parameter input or using predefined parameters. 常见最佳做法是不在 PowerShell 脚本 中包含机密(不包含密码等)。Common best practice is not to include secrets in your PowerShell scripts (no passwords, etc.). 详细了解 PowerShell 脚本安全性Learn more about PowerShell script security

“运行脚本”的创建者和审批者Run Script authors and approvers

“运行脚本”使用了“脚本创建者”和“脚本审批者”的概念,分别作为实现脚本和执行脚本的角色 。Run Scripts uses the concept of script authors and script approvers as separate roles for implementation and execution of a script. 将创建者和审批者两个角色进行区分,使“运行脚本”这个强大的工具能够进行重要过程检查。Having the author and approver roles separated allows an important process check for the powerful tool that Run Scripts is. 还有“脚本运行器” 角色,它允许执行脚本,但不允许创建或审批脚本。There's an additional script runners role that allows execution of scripts, but not creation or approval of scripts. 请参阅为脚本创建安全角色See Create security roles for scripts.

脚本角色控制Scripts roles control

默认情况下,用户不能批准他们创建的脚本。By default, users can't approve a script they've authored. 由于这些脚本功能非常强大、用途多样,并且可能部署到多个设备,因此可以将脚本创建者和脚本批准者之间的角色相互分开。Because scripts are powerful, versatile, and potentially deployed to many devices, you can separate the roles between the person that authors the script and the person that approves the script. 这些角色可以额外提升安全级别,避免在没有监督的情况下运行脚本。These roles give an additional level of security against running a script without oversight. 可以禁用辅助审批,以方便进行测试。You're able to turn off secondary approval, for ease of testing.

“批准”或“拒绝”脚本Approve or Deny a script

脚本必须被“脚本审批者”角色批准,然后才能运行 。Scripts must be approved, by the script approver role, before they can be run. 批准脚本:To approve a script:

  1. 在 Configuration Manager 控制台中,单击“软件库” 。In the Configuration Manager console, click Software Library.
  2. 在“软件库” 工作区中,单击“脚本” 。In the Software Library workspace, click Scripts.
  3. 在“脚本” 列表中,选择想要批准或拒绝的脚本,然后在“主页” 选项卡“脚本” 组中,单击“批准/拒绝” 。In the Script list, choose the script you want to approve or deny and then, on the Home tab, in the Script group, click Approve/Deny.
  4. 在“批准或拒绝脚本”对话框中,选择“批准”或“拒绝”脚本 。In the Approve or deny script dialog box, select Approve, or Deny for the script. (可选)输入有关你决定的注释。Optionally, enter a comment about your decision. 如果你拒绝脚本,它将无法在客户端设备上运行。If you deny a script, it can't be run on client devices.
    脚本 - 批准Script - Approval
  5. 完成向导。Complete the wizard. 在“脚本” 列表中可以看到“批准状态” 列中发生的变化,具体要取决于你执行的操作。In the Script list, you see the Approval State column change depending on the action you took.

允许用户批准他们自己的脚本Allow users to approve their own scripts

此批准主要用于脚本开发中的测试阶段。This approval is primarily used for the testing phase of script development.

  1. 在 Configuration Manager 控制台中,单击“管理” 。In the Configuration Manager console, click Administration.
  2. 在“管理” 工作区中,展开“站点配置” ,然后单击“站点” 。In the Administration workspace, expand Site Configuration, and then click Sites.
  3. 在站点列表中,选择你的站点,然后在“主页” 选项卡的“站点” 组中,单击“层次结构设置” 。In the list of sites, choose your site and then, on the Home tab, in the Sites group, click Hierarchy Settings.
  4. 在“层次结构设置属性”对话框的“常规”选项卡上,清除“脚本创建者需要其他脚本审批者”复选框 。On the General tab of the Hierarchy Settings Properties dialog box, clear the checkbox Script authors require additional script approver.

重要

最好是不允许脚本创建者审批自己的脚本。As a best practice, you shouldn't allow a script author to approve their own scripts. 仅实验室环境中允许此操作。It should only be allowed in a lab setting. 请仔细考虑在生产环境中更改此设置的潜在影响。Carefully consider the potential impact of changing this setting in a production environment.

安全作用域Security scopes

“运行脚本”使用安全作用域(Configuration Manager 中的一项现有功能)通过分配代表用户组的标记来控制脚本的创作和执行。Run Scripts uses security scopes, an existing feature of Configuration Manager, to control scripts authoring and execution through assigning tags that represent user groups. 有关使用安全作用域的详细信息,请参阅为 Configuration Manager 配置基于角色的管理For more information on using security scopes, see Configure role-based administration for Configuration Manager.

为脚本创建安全角色Create security roles for scripts

默认情况下,三个用于运行脚本的安全角色不是在 Configuration Manager 中创建的。The three security roles used for running scripts aren't created by default in Configuration Manager. 要创建脚本运行者、脚本创建者和脚本审批者角色,请执行下列所述步骤。To create the script runners, script authors, and script approvers roles, follow the outlined steps.

  1. 在 Configuration Manager 控制台中,转到“管理” >“安全性” >“安全角色” In the Configuration Manager console, go to Administration >Security >Security Roles
  2. 右键单击一个角色,然后单击“复制” 。Right-click on a role and click Copy. 复制的角色已分配有权限。The role you copy has permissions already assigned. 确保仅获取所需的权限。Make sure you take only the permissions that you want.
  3. 为自定义角色提供“名称”和“说明” 。Give the custom role a Name and a Description.
  4. 按如下所述为安全角色分配权限。Assign the security role the permissions outlined below.

安全角色权限Security Role Permissions

角色名称:脚本运行者Role Name: Script Runners

  • 描述:这些权限仅允许此角色运行之前创建且已由其他角色批准的脚本。Description: These permissions enable this role to only run scripts that were previously created and approved by other roles.
  • 权限: 请确保将以下项设置为“是” 。Permissions: Ensure the following are set to Yes.
类别Category 权限Permission 状态State
收集Collection 运行脚本Run Script Yes
站点Site 读取Read Yes
SMS 脚本SMS Scripts 读取Read Yes

角色名称:脚本创建者Role Name: Script Authors

  • 描述:这些权限允许此角色编写脚本,但不能批准或运行脚本。Description: These permissions enable this role to author scripts, but they can't approve or run them.
  • 权限:请确保设置以下权限。Permissions: Ensure the following permissions are set.
类别Category 权限Permission 状态State
收集Collection 运行脚本Run Script No
站点Site 读取Read Yes
SMS 脚本SMS Scripts 创建Create Yes
SMS 脚本SMS Scripts 读取Read Yes
SMS 脚本SMS Scripts 删除Delete Yes
SMS 脚本SMS Scripts 修改Modify Yes

角色名称:脚本审批者Role Name: Script Approvers

  • 描述:这些权限允许此角色批准脚本,但不能创建或运行脚本。Description: These permissions enable this role to approve scripts, but they can't create or run them.
  • 权限: 请确保设置以下权限。Permissions: Ensure the following permissions are set.
类别Category 权限Permission 状态State
收集Collection 运行脚本Run Script No
站点Site 读取Read Yes
SMS 脚本SMS Scripts 读取Read Yes
SMS 脚本SMS Scripts 批准Approve Yes
SMS 脚本SMS Scripts 修改Modify Yes

脚本创建者角色的 SMS 脚本权限示例Example of SMS Scripts permissions for the script authors role

脚本创建者角色的 SMS 脚本权限示例

创建脚本Create a script

  1. 在 Configuration Manager 控制台中,单击“软件库” 。In the Configuration Manager console, click Software Library.
  2. 在“软件库” 工作区中,单击“脚本” 。In the Software Library workspace, click Scripts.
  3. 在“主页” 选项卡的“创建” 组中,单击“创建脚本” 。On the Home tab, in the Create group, click Create Script.
  4. 在“创建脚本” 向导的“脚本” 页上,配置以下设置:On the Script page of the Create Script wizard, configure the following settings:
    • 脚本名称 - 输入脚本的名称。Script Name - Enter a name for the script. 虽然可以创建具有相同名称的多个脚本,但使用重复名称会让你难以在 Configuration Manager 控制台中查找所需的脚本。Although you can create multiple scripts with the same name, using duplicate names makes it harder for you to find the script you need in the Configuration Manager console.
    • 脚本语言 - 目前,仅支持 PowerShell 脚本。Script language - Currently, only PowerShell scripts are supported.
    • 导入 - 将 PowerShell 脚本导入到控制台。Import - Import a PowerShell script into the console. 该脚本将在“脚本” 字段中显示。The script is displayed in the Script field.
    • 清除 - 从“脚本”字段中删除当前脚本。Clear - Removes the current script from the Script field.
    • 脚本 - 显示当前导入的脚本。Script - Displays the currently imported script. 你可以在此字段中根据需要编辑脚本。You can edit the script in this field as necessary.
  5. 完成向导。Complete the wizard. 新脚本将显示在“脚本” 列表,且状态显示为“正等待审批” 。The new script is displayed in the Script list with a status of Waiting for approval. 在客户端设备上运行此脚本之前,必须先批准它。Before you can run this script on client devices, you must approve it.

重要

避免在使用“运行脚本”功能时编写设备重启或 Configuration Manager 代理重启的脚本。Avoid scripting a device reboot or a restart of the Configuration Manager agent when using the Run Scripts feature. 执行此操作可能会导致连续的重启状态。Doing so could lead to a continuous rebooting state. 我们对启用重启设备的客户端通知功能进行了增强,供你需要时使用。If needed, there are enhancements to the client notification feature that enable restarting devices. 等待重新启动列可以帮助识别需要重启的设备。The pending restart column can help identify devices that need a restart.

脚本参数Script parameters

将参数添加到脚本可以为你的工作提供更高的灵活性。Adding parameters to a script provides increased flexibility for your work. 最多只能包含 10 个参数。You can include up to 10 parameters. 以下内容概述“运行脚本”功能的当前功能以及“字符串”和“整数”数据类型的脚本参数 。The following outlines the Run Scripts feature's current capability with script parameters for; String, Integer data types. 也提供了预设值列表。Lists of preset values are also available. 如果脚本具有不受支持的数据类型,你将收到一个警告。If your script has unsupported data types, you get a warning.

在“创建脚本” 对话框中,单击“脚本”下的“脚本参数” 。In the Create Script dialog, click Script Parameters under Script.

你的每一个脚本参数都有自己的对话框,用于添加更多细节以及验证信息。Each of your script's parameters has its own dialog for adding further details and validation. 如果脚本中有默认参数,它将在参数 UI 中进行枚举,你可以设置它。If there's a default parameter in the script, it will be enumerated in the parameter UI and you can set it. Configuration Manager 不会覆盖默认值,因为它永远不会直接修改脚本。Configuration Manager won't overwrite the default value since it will never modify the script directly. 可以将默认值视为 UI 中提供的“预先填充的建议值”,但 Configuration Manager 在运行时不提供对默认值的访问。You can think of this as "pre-populated suggested values" are provided in the UI, but Configuration Manager doesn't provide access to "default" values at run-time. 这可以通过编辑脚本以具有正确的默认值来解决。This can be worked around by editing the script to have the correct defaults.

重要

参数值不能包含单引号。Parameter values can't contain a single quote.

存在一个已知问题,其中包含的或以单引号引起来的参数值不会正确传递给脚本。There is a known issue where parameter values that include or are enclosed in single quotes don't get passed to the script properly. 指定在脚本中包含空格的默认参数值时,请改用双引号。When specifying default parameter values containing a space within a script, use double quotes instead. 在创建或执行脚本期间指定默认参数值时,无论该值是否包含空格,都无需用单引号或双引号将默认值引起来 。When specifying default parameter values during creation or execution of a Script, surrounding the default value in either double or single quotes is not necessary regardless of whether the value contains a space or not.

参数验证Parameter validation

脚本中的每一个参数都有一个“脚本参数属性”对话框,可以在此处添加该参数的验证信息 。Each parameter in your script has a Script Parameter Properties dialog for you to add validation for that parameter. 在添加验证后,若要输入不符合验证要求的参数值,应该会看到错误。After adding validation, you should get errors if you're entering a value for a parameter that doesn't meet its validation.

例如:FirstNameExample: FirstName

在此示例中,可以设置字符串参数 FirstName 的属性。In this example, you're able to set the properties of the string parameter, FirstName.

脚本参数 - 字符串

“脚本参数属性” 对话框的验证部分包含供你使用的以下字段:The validation section of the Script Parameter Properties dialog contains the following fields for your use:

  • 最小长度 - FirstName 字段的最小字符数。Minimum Length - minimum number of characters of the FirstName field.
  • 最大长度 - FirstName 字段的最大字符数Maximum Length- maximum number of characters of the FirstName field
  • RegEx - 正则表达式 的简称。RegEx - short for Regular Expression. 有关使用正则表达式的详细信息,请参阅下一部分中的“使用正则表达式验证” 。For more information on using the Regular Expression, see the next section, Using Regular Expression validation.
  • 自定义错误 - 用于添加你自己的自定义错误消息,该消息将取代任何系统验证错误消息。Custom Error - useful for adding your own custom error message that supersedes any system validation error messages.

使用正则表达式验证Using Regular Expression validation

正则表达式是一种紧凑的编程形式,用于检查一串字符的编码验证。A regular expression is a compact form of programming for checking a string of characters against an encoded validation. 例如,可以通过将 [^A-Z] 放入“RegEx” 字段,来检查“FirstName” 字段中是否缺少大写字母字符。For example, you could check for the absence of a capital alphabetic character in the FirstName field by placing [^A-Z] in the RegEx field.

.NET Framework 支持此对话框的正则表达式处理。The regular expression processing for this dialog is supported by the .NET Framework. 有关使用正则表达式的指南,请参阅 .NET 正则表达式正则表达式语言For guidance on using regular expressions, see .NET Regular Expression and Regular Expression Language.

脚本示例Script examples

以下几个示例对你希望用于此功能的脚本进行了说明。Here are a couple examples that illustrate scripts you might want to use with this capability.

创建新文件夹和文件Create a new folder and file

如果提供命名输入,此脚本会在文件夹中创建一个新文件夹和一个文件。This script creates a new folder and a file within the folder, given your naming input.

Param(
[Parameter(Mandatory=$True)]
[string]$FolderName,
[Parameter(Mandatory=$True)]
[string]$FileName
)

New-Item $FolderName -type directory
New-Item $FileName -type file

获取 OS 版本Get OS Version

此脚本使用 WMI 来查询计算机的 OS 版本。This script uses WMI to query the machine for its OS version.

Write-Output (Get-WmiObject -Class Win32_operatingSystem).Caption

编辑或复制 PowerShell 脚本Edit or copy PowerShell scripts

可以编辑 或复制 用于“运行脚本” 功能的现有 PowerShell 脚本。You can Edit or Copy an existing PowerShell script used with the Run Scripts feature. 现在可以直接编辑想要更改的脚本,而无需重新创建脚本。Instead of recreating a script that you need to change, now directly edit it. 这两种操作都使用与创建新脚本时所使用的相同向导体验。Both actions use the same wizard experience as when you create a new script. 编辑或复制脚本时,Configuration Manager 不会保留审批状态。When you edit or copy a script, Configuration Manager doesn't persist the approval state.

提示

请勿编辑在客户端上主动运行的脚本。Don't edit a script that's actively running on clients. 它们不会完成原始脚本的运行,并且可能无法从这些客户端获取预期的结果。They won't finish running the original script, and you may not get the intended results from these clients.

编辑脚本Edit a script

  1. 转到“软件库”工作区下的“脚本”节点 。Go to the Scripts node under the Software Library workspace.
  2. 选择要编辑的脚本,然后在功能区中单击“编辑” 。Select the script to edit, then click Edit in the ribbon.
  3. 在“脚本详细信息”页中更改或重新导入脚本 。Change or reimport your script in the Script Details page.
  4. 单击“下一步”,查看“摘要”,然后在完成编辑后单击“关闭” 。Click Next to view the Summary then Close when you're finished editing.

复制脚本Copy a script

  1. 转到“软件库”工作区下的“脚本”节点 。Go to the Scripts node under the Software Library workspace.
  2. 选择要复制的脚本,然后单击功能区中的“复制” 。Select the script to copy, then click Copy in the ribbon.
  3. 重命名“脚本名”字段中的脚本,并进行可能需要的任何其他编辑 。Rename the script in the Script name field and make any additional edits you may need.
  4. 单击“下一步”,查看“摘要”,然后在完成编辑后单击“关闭” 。Click Next to view the Summary then Close when you're finished editing.

运行脚本Run a script

经批准后,脚本就可以在单个设备或集合上运行。After a script is approved, it can be run against a single device or a collection. 在脚本开始执行后,它便会通过一小时超时的高优先级系统快速启动。Once execution of your script begins, it's launched quickly through a high priority system that times-out in one hour. 然后使用状态消息系统返回脚本结果。The results of the script are then returned using a state message system.

要为脚本选择目标集合:To select a collection of targets for your script:

  1. 在 Configuration Manager 控制台中,单击“资产和符合性” 。In the Configuration Manager console, click Assets and Compliance.
  2. 在“资产和符合性”工作区中,单击“设备集合” 。In the Assets and Compliance workspace, click Device Collections.
  3. 在“设备集合” 列表中,单击要在其中运行脚本的设备集合。In the Device Collections list, click the collection of devices on which you want to run the script.
  4. 选择所选集合,单击“运行脚本” 。Select a collection of your choice, click Run Script.
  5. 在“运行脚本” 向导的“脚本” 页,从列表中选择一个脚本。On the Script page of the Run Script wizard, choose a script from the list. 仅显示已批准的脚本。Only approved scripts are shown.
  6. 单击“下一步” ,然后完成向导。Click Next, and then complete the wizard.

重要

如果脚本未运行(例如,因为目标设备关闭),则在这一小时内你必须再次运行。If a script does not run, for example because a target device is turned off during the one hour time period, you must run it again.

目标计算机执行Target machine execution

脚本在目标客户端上作为“系统”或“计算机”帐户执行 。The script is executed as the system or computer account on the targeted client(s). 此帐户的网络访问权限是受限的。This account has limited network access. 必须对通过该脚本进行的任何远程系统访问和访问位置进行相应的预配置。Any access to remote systems and locations by the script must be provisioned accordingly.

脚本监视Script monitoring

在设备集合上启动脚本运行以后,请使用采用以下过程来监视该操作。After you have initiated running a script on a collection of devices, use the following procedure to monitor the operation. 可在脚本执行时进行实时监视,稍后回去查看某个给定“运行脚本”执行的状态和结果。You are able to monitor a script in real time as it executes, and later return to the status and results for a given Run Script execution. 脚本状态数据将作为删除过期客户端操作维护任务的一部分或删除脚本任务的一部分进行清理。Script status data is cleaned up as part of the Delete Aged Client Operations maintenance task or deletion of the script.

脚本监视器 - 脚本运行状态

  1. 在 Configuration Manager 控制台中,单击“监视” 。In the Configuration Manager console, click Monitoring.

  2. 在“监视” 工作区中,单击“脚本状态” 。In the Monitoring workspace, click Script Status.

  3. 在“脚本状态” 列表中,可以查看在客户端设备上运行的每个脚本的结果。In the Script Status list, you view the results for each script you ran on client devices. 脚本退出代码为“0” 通常表示脚本已成功运行。A script exit code of 0 generally indicates that the script ran successfully.

    脚本监视器 - 截断的脚本

脚本输出Script output

客户端采用 JSON 格式返回脚本输出,将脚本的结果通过管道传输至 ConvertTo-Json cmdlet。Client's return script output using JSON formatting by piping the script's results to the ConvertTo-Json cmdlet. JSON 格式会始终如一地返回可读的脚本输出。The JSON format consistently returns readable script output. 对于不以输出形式返回对象的脚本,ConvertTo-Json cmdlet 会将输出转换为客户端返回的简单字符串,而不是返回 JSON。For scripts that do not return objects as output, the ConvertTo-Json cmdlet converts the output to a simple string that the client returns instead of JSON.

  • 收到未知结果或客户端脱机的脚本不会在图表或数据集中显示。Scripts that get an unknown result, or where the client was offline, won't show in the charts or data set.

  • 避免返回大型脚本输出,因为它会截断为 4KB。Avoid returning large script output since it's truncated to 4 KB.

  • 将脚本中的枚举对象转换为字符串值,这样它们就能以 JSON 格式正确显示。Convert an enum object to a string value in scripts so they're properly displayed in JSON formatting.

    将枚举对象转换为字符串值

可以原始或结构化 JSON 格式查看详细的脚本输出。You can view detailed script output in raw or structured JSON format. 此格式设置可使输出更易于读取和分析。This formatting makes the output easier to read and analyze. 如果脚本返回有效的 JSON 格式的文本,或者输出可使用 ConvertTo-Json PowerShell cmdlet 转换为 JSON,则以 JSON 输出或原始输出的形式查看详细输出 。If the script returns valid JSON-formatted text or the output can be converted to JSON using the ConvertTo-Json PowerShell cmdlet, then view the detailed output as either JSON Output or Raw Output. 否则,唯一的选择是脚本输出 。Otherwise the only option is Script Output.

例如:脚本输出可转换为有效的 JSONExample: Script output is convertible to valid JSON

命令:$PSVersionTable.PSVersionCommand: $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      16299  551

例如:脚本输出是无效的 JSONExample: Script output isn't valid JSON

命令:Write-Output (Get-WmiObject -Class Win32_OperatingSystem).CaptionCommand: Write-Output (Get-WmiObject -Class Win32_OperatingSystem).Caption

Microsoft Windows 10 Enterprise

日志文件Log files

  • 在客户端上,默认情况下位于 C:\Windows\CCM\logs 中:On the client, by default in C:\Windows\CCM\logs:

    • Scripts.logScripts.log
    • CcmMessaging.logCcmMessaging.log
  • 在 MP 上,默认情况下位于 C:\SMS_CCM\Logs 中:On the MP, by default in C:\SMS_CCM\Logs:

    • MP_RelayMsgMgr.log MP_RelayMsgMgr.log
  • 在站点服务器上,默认情况下位于 C:\Program Files\Configuration Manager\Logs 中:On the site server, by default in C:\Program Files\Configuration Manager\Logs:

    • SMS_Message_Processing_Engine.log SMS_Message_Processing_Engine.log

另请参阅See Also