当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 谁在运行您的 PowerShell 脚本?

作者:精品下载站 日期:2024-12-14 07:52:08 浏览:13 分类:玩电脑

谁在运行您的 PowerShell 脚本?


我经常谈论从一开始就在 PowerShell 脚本和函数中包含详细输出的好处。当其他人运行您的命令但遇到问题时,这尤其有用。您可以让他们开始记录,使用 -Verbose 运行命令,关闭记录并将其发送给您。如果您编写了内容丰富的详细消息,您应该能够找出问题。部分信息可能包括有关运行命令的人员及其环境的元数据。简单来说,我创建了一个易于使用的函数,名为 Get-PSWho。

该函数将自定义对象写入管道,其中包含有关用户、其计算机及其 PowerShell 环境的信息。可能有用的一件事是了解命令在哪里运行,因为并非所有 PowerShell 主机都支持您的代码。

[玩转系统] 谁在运行您的 PowerShell 脚本?

[玩转系统] 谁在运行您的 PowerShell 脚本?

[玩转系统] 谁在运行您的 PowerShell 脚本?

我做出的另一个设计决定是稍微偏离建议的做法,即仅将一种类型的对象写入管道而不是重新发明轮子。我写这篇文章的原因之一是为了使其能够轻松地合并到您的函数和脚本中。您可以嵌入该函数或确保它与您的命令一起分发,以便您可以将输出包含在 Write-Verbose 行中。但是,您必须将输出转换为字符串。

Write-Verbose "User Metadata"
Write-Verbose (Get-PSWho | Out-String)

由于无论如何您可能都需要一个字符串,因此我决定将其作为选项包含在内。

Write-Verbose "User Metadata"
Get-PSWho -AsString | Write-Verbose

[玩转系统] 谁在运行您的 PowerShell 脚本?

或者您也可以轻松地将字符串版本通过管道传输到 Write-Host。重点是我考虑了如何使用这个命令并添加了我认为有用的参数。同样,我对这是否是正确的设计决策感到有点困惑,但您可以简单地选择采用我的代码并将其直接粘贴到您的函数中。

您可以在 GitHub 上找到代码。

PSWho.ps1:

Function Get-PSWho {
   Get-PSWho

  User            : BOVINE320\Jeff
  Elevated        : True
  Computername    : BOVINE320
  OperatingSystem : Microsoft Windows 10 Pro [64-bit]
  OSVersion       : 10.0.16299
  PSVersion       : 5.1.16299.64
  Edition         : Desktop
  PSHost          : ConsoleHost
  WSMan           : 3.0
  ExecutionPolicy : RemoteSigned
  Culture         : en-US

  .EXAMPLE
  PS /mnt/c/scripts> get-pswho


  User            : jhicks
  Elevated        : NA
  Computername    : Bovine320
  OperatingSystem : Linux 4.4.0-43-Microsoft #1-Microsoft Wed Dec 31 14:42:53 PST 2014
  OSVersion       : Ubuntu 16.04.3 LTS
  PSVersion       : 6.0.0-rc
  Edition         : Core
  PSHost          : ConsoleHost
  WSMan           : 3.0
  ExecutionPolicy : Unrestricted
  Culture         : en-US

  .EXAMPLE
  PS C:\Program Files\PowerShell.0.0-rc> get-pswho


  User            : BOVINE320\Jeff
  Elevated        : True
  Computername    : BOVINE320
  OperatingSystem : Microsoft Windows 10 Pro [64-bit]
  OSVersion       : 10.0.16299
  PSVersion       : 6.0.0-rc
  Edition         : Core
  PSHost          : ConsoleHost
  WSMan           : 3.0
  ExecutionPolicy : RemoteSigned
  Culture         : en-US

  .EXAMPLE
  PS C:\> Get-PSWho -asString | Set-Content c:\test\who.txt

  .NOTES
  Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/

   .INPUTS
   none

   .OUTPUTS
   [pscustomboject]
   [string]
  
   .LINK
  Get-CimInstance
  .LINK
  Get-ExecutionPolicy
  .LINK
  $PSVersionTable
  .LINK
  $Host
  #>
    [CmdletBinding()]
    Param(
      [switch]$AsString
    )

if ($PSVersionTable.PSEdition -eq "desktop" -OR $PSVersionTable.OS -match "Windows") {
      
        #get some basic information about the operating system
        $cimos = Get-CimInstance win32_operatingsystem -Property Caption, Version,OSArchitecture
        $os = "$($cimos.Caption) [$($cimos.OSArchitecture)]"
        $osver = $cimos.Version

        #determine the current user so we can test if the user is running in an elevated session
        $current = [Security.Principal.WindowsIdentity]::GetCurrent()
        $principal = [Security.Principal.WindowsPrincipal]$current
        $Elevated = $principal.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
        $user = $current.Name
        $computer = $env:COMPUTERNAME
    }
    else {
     #non-Windows values
      $os = $PSVersionTable.OS
      $lsb = lsb_release -d
      $osver =   ($lsb -split ":")[1].Trim()
      $elevated = "NA"
      $user = $env:USER
      $computer = $env:NAME
    }


    #object properties will be displayed in the order they are listed here
    $who = [pscustomObject]@{ 
        User            = $user
        Elevated        = $elevated
        Computername    = $computer
        OperatingSystem = $os
        OSVersion       = $osver
        PSVersion       = $PSVersionTable.PSVersion.ToString()
        Edition         = $PSVersionTable.PSEdition
        PSHost          = $host.Name
        WSMan           = $PSVersionTable.WSManStackVersion.ToString()
        ExecutionPolicy = (Get-ExecutionPolicy)
        Culture         = $host.CurrentCulture
    }

    if ($AsString) {
      $who | Out-String
    }
    else {
      $who
    }
} #end Get-PSWho 

由于我使用的是 Get-CimInstance,因此该命令不会在非 Windows 系统上的 PowerShell Core 中运行。尽管我可能可以考虑到这一点并围绕它编写代码。

如果您遇到问题或有建议,请在 Gist 页面上发布问题。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯