Powershell 命令未发现异常

2024-05-31

我有以下 powershell 脚本,它使用一个函数并且可以在 ISE 中正常运行:

Get-ComputerSessions -Computer "localhost"

# ===========================================================================
# Functions
# ===========================================================================


Function Get-ComputerSessions {
<#
.SYNOPSIS
    Retrieves tall user sessions from local or remote server/s
.DESCRIPTION
    Retrieves tall user sessions from local or remote server/s
.PARAMETER computer
    Name of computer/s to run session query against.
.NOTES
    Name: Get-ComputerSessions
    Author: Boe Prox
    DateCreated: 01Nov2010
 
.LINK
 
https://boeprox.wordpress.org
 
.EXAMPLE
Get-ComputerSessions -computer "server1"
 
Description
-----------
This command will query all current user sessions on 'server1'.
 
#>
[cmdletbinding(
    DefaultParameterSetName = 'session',
    ConfirmImpact = 'low'
)]
    Param(
        [Parameter(
            Mandatory = $True,
            Position = 0,
            ValueFromPipeline = $True)]
            [string[]]$computer
            )
Begin {
    $report = @()
    }
Process {
    ForEach($c in $computer) {
        # Parse 'query session' and store in $sessions:
        $sessions = query session /server:$c
            1..($sessions.count -1) | % {
                $temp = "" | Select Computer,SessionName, Username, Id, State, Type, Device
                $temp.Computer = $c
                $temp.SessionName = $sessions[$_].Substring(1,18).Trim()
                $temp.Username = $sessions[$_].Substring(19,20).Trim()
                $temp.Id = $sessions[$_].Substring(39,9).Trim()
                $temp.State = $sessions[$_].Substring(48,8).Trim()
                $temp.Type = $sessions[$_].Substring(56,12).Trim()
                $temp.Device = $sessions[$_].Substring(68).Trim()
                $report += $temp
            }
        }
    }
End {
    $report
    }
}

但是,如果我尝试从 powershell 命令行运行此命令,则会收到以下错误:

PS C:\Downloads\Powershell> C:\Downloads\Powershell\test.ps1
The term 'Get-ComputerSessions' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path
 is correct and try again.
At C:\Downloads\Powershell\test.ps1:25 char:21
+ Get-ComputerSessions <<<<  -Computer "localhost"
    + CategoryInfo          : ObjectNotFound: (Get-ComputerSessions:String) [], CommandNotFoundExc
   eption
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\Downloads\Powershell>

谁能告诉我哪里出了问题,我需要将函数放在单独的模块中吗?

我的背景是 VB 脚本,所以我很难掌握逻辑。


移动此呼叫:

Get-ComputerSessions -Computer "localhost"

下面定义函数 Get-ComputerSession 并重试。 PowerShell 在定义函数之前遇到函数调用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Powershell 命令未发现异常 的相关文章

随机推荐