Powershell CommandNotFoundException [英] Powershell CommandNotFoundException

查看:197
本文介绍了Powershell CommandNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下使用功能的powershell脚本,它将在ISE中运行正常:

I have the following powershell script which uses a function and will run OK within 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命令行运行此命令,则会出现以下错误:

If I attempt to run this from a powershell command line however, I get the following error:

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>

谁能告诉我我要去哪里,我是否需要将该功能放在单独的模块中?

Can anyone tell me where I am going wrong, do I need to put the function in a separate module?

我的背景是VB脚本,所以我在努力掌握逻辑.

My background is VB script so I am struggling to get to grips with the logic.

推荐答案

移动此呼叫:

Get-ComputerSessions -Computer "localhost"

在功能Get-ComputerSession的定义下方,然后重试.在定义函数之前,PowerShell会遇到函数调用.

Down below the definition of the function Get-ComputerSession and try again. PowerShell is encountering the function invocation before you have defined the function.

这篇关于Powershell CommandNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆