【问题标题】:Powershell Control Console OutputPowershell 控制台输出
【发布时间】:2020-12-08 06:21:46
【问题描述】:

我正在整理一个正在运行的 PowerShell 脚本,但如果可能的话,我只需要一个小元素的帮助。让我告诉你...

脚本

# Update PC Descritpions.ps1
# v1.0
# J-D06-Produce
$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath
Import-Csv .\computer_desc.csv | ForEach {
    $OSValues = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $_.Server
    $OSValues.Description = $_.Description
    ""
    write-host "Old Description $OSValues"
    (gwmi win32_operatingsystem -computer $_.Server).description
    $OSValues.Put() >$null 2>&1
    ""
    write-host "New Description $OSValues" 
    (gwmi win32_operatingsystem -computer $_.Server).description   
}  

它循环遍历一个 CSV 文件以针对计算机名称迭代新的计算机描述。 我总是喜欢为用户改进控制台窗口中的输出,以便轻松查看正在发生的事情。当前输出与我想要的非常接近...看​​看

Old Description \\MUNGO-BONGO\root\cimv2:Win32_OperatingSystem=@
GP-B83409-EGTON CLINIC RM 1

New Description \\MUNGO-BONGO\root\cimv2:Win32_OperatingSystem=@
test1

Old Description \\NECS0983DRTY\root\cimv2:Win32_OperatingSystem=@
GP-B83409-EGTON CLINIC RM 2

New Description \\NECS0983DRTY\root\cimv2:Win32_OperatingSystem=@
test2

Old Description \\ELITE-DESKTOP\root\cimv2:Win32_OperatingSystem=@
GP-B83409-EGTON CLINIC RM 3

New Description \\ELITE-DESKTOP\root\cimv2:Win32_OperatingSystem=@
test3 

所以,我可以看到 PC 名称、旧描述以及它的更改内容。 但是,如果可能的话,我想去掉这个 "\root\cimv2:Win32_OperatingSystem=@" 让它更整洁。 类似于 ...

Old Description for MUNGO-BONGO
GP-B83409-EGTON CLINIC RM 1

New Description for MUNGO-BONGO
test1

Old Description for NECS0983DRTY
GP-B83409-EGTON CLINIC RM 2

New Description for NECS0983DRTY
test2

Old Description for ELITE-DESKTOP
GP-B83409-EGTON CLINIC RM 3

New Description for ELITE-DESKTOP
test3

非常感谢您的任何建议。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    改变这些

    write-host "New Description $OSValues"
    

    到这里

    write-host New Description for $OSValues.csname
    

    或者这个

    write-host New Description for $OSValues.pscomputername
    

    如果出于其他原因您需要使用双引号,则只需将 $OSValues.csname$( ) 括起来

    write-host "New Description for $($OSValues.csname)"
    write-host "New Description for $($OSValues.pscomputername)"
    

    【讨论】:

    • 完美,谢谢先生。我现在正在学习更多,如果您有时间,这些更改是如何工作的?谢谢。
    • 因此,有两件非常重要的事情需要学习,那就是如何检查对象。如果您使用 $OSvalues 行并运行它,您可以使用该变量并检查它。试试这些命令,看看你得到的所有信息。 $OSvalues | Format-List *$OSvalues | Get-Member 第一个将显示属性和值,第二个将显示有关对象类型的属性和信息。您可以看到 csname 和 pscomputername 都是对象上具有相同值(计算机名称)的属性
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 2013-09-17
    • 1970-01-01
    • 2010-12-22
    • 2021-08-27
    • 2019-07-26
    • 1970-01-01
    相关资源
    最近更新 更多