使用批处理运行远程PowerShell V1.0命令

使用批处理运行远程PowerShell V1.0命令,powershell,scripting,batch-file,hyper-v,Powershell,Scripting,Batch File,Hyper V,我正在尝试从我的Hyper-V主机收集一些信息。我有一大堆,我想自动化这个过程。 我需要获取每个主机上运行的虚拟机。 我想从批处理脚本中执行此操作。 当我在PowerShell V1.0窗口(在Hyper-V主机上)中运行此命令时,它会工作并提供必要的信息: get-vmmemory | select VMelementName,reservation | out-file c:\Output.txt 以下是我如何从批处理脚本运行此脚本: \\<RemoteMachine>\c$\

我正在尝试从我的Hyper-V主机收集一些信息。我有一大堆,我想自动化这个过程。 我需要获取每个主机上运行的虚拟机。 我想从批处理脚本中执行此操作。 当我在PowerShell V1.0窗口(在Hyper-V主机上)中运行此命令时,它会工作并提供必要的信息:

get-vmmemory | select VMelementName,reservation | out-file c:\Output.txt
以下是我如何从批处理脚本运行此脚本:

\\<RemoteMachine>\c$\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command get-vmmemory >>aa.txt
\\\c$\Windows\System32\WindowsPowerShell\v1.0\powershell.exe-命令get-vmmemory>>aa.txt
这是我得到的输出

The term 'get-vmmemory' 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 line:1 char:16
+ & {get-vmmemory <<<< }
    + CategoryInfo          : ObjectNotFound: (get-vmmemory:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
术语“get-vmmemory”无法识别为cmdlet、函数、,
脚本文件或可操作程序。检查名称的拼写,或者检查路径是否正确
已包括,请验证路径是否正确,然后重试。
第1行字符:16

+&{get-vmmemoryget-vmmemory不是标准cmdlet。如果运行powershell的计算机上没有该cmdlet,则会出现此错误


您可能需要运行导入模块以确保将vmware Ps模块加载到会话中。

get-vmmemory不是标准cmdlet。如果运行powershell的计算机上没有该cmdlet,则会出现此错误


您可能需要运行导入模块以确保vmware Ps模块已加载到会话中。

当我在运行hyper v(非VM)的机器上的PowerShell V1.0控制台上键入get-vmmemory时,该命令会起作用。

当我在运行hyper v(非VM)的机器上的PowerShell V1.0控制台上键入get-vmmemory时该命令有效。

Get-VMMemory
不是标准/内置cmdlet-它是Hyper-V模块的一部分。因此,这里确实存在两个问题:

  • 如果您确实在运行PowerShell v1.0(EXE位于1.0目录中,但所有版本都在同一个位置),则该模块可能甚至无法工作
  • 您没有加载模块,或者在运行批处理文件的位置没有可用的模块
  • 对于#1,请按以下方式检查版本: \c$\Windows\System32\WindowsPowerShell\v1.0\powershell.exe“写入主机$psversiontable.psversion”

    旁白:为什么要使用UNC路径指向本地计算机的C驱动器

    对于#2,您需要将代码包装在PS1文件中,并告诉powershell.exe执行该文件,以便您可以导入模块。powershell 3.0会自动加载模块,但我不知道在您这样执行cmdlet时是否会这样做

    import-module Hyper-V
    get-vmmemory | select VMelementName,reservation | out-file c:\Output.txt
    
    powershell.exe-f myscript.ps1

    编辑:根据您的评论,这两个都不起作用。您需要使用PSRemoting在远程系统上从您的系统执行命令

    invoke-command -computername hypervhost -scriptblock {get-vmmemory | select VMelementName,reservation
    

    以上内容未经测试。如果此操作不起作用,则需要在您的环境中设置PSRemoting。

    Get-VMMemory
    不是标准/内置cmdlet-它是Hyper-V模块的一部分。因此,这里确实存在两个问题:

  • 如果您确实在运行PowerShell v1.0(EXE位于1.0目录中,但所有版本都在同一个位置),则该模块可能甚至无法工作
  • 您没有加载模块,或者在运行批处理文件的位置没有可用的模块
  • 对于#1,请按以下方式检查版本: \c$\Windows\System32\WindowsPowerShell\v1.0\powershell.exe“写入主机$psversiontable.psversion”

    旁白:为什么要使用UNC路径指向本地计算机的C驱动器

    对于#2,您需要将代码包装在PS1文件中,并告诉powershell.exe执行该文件,以便您可以导入模块。powershell 3.0会自动加载模块,但我不知道在您这样执行cmdlet时是否会这样做

    import-module Hyper-V
    get-vmmemory | select VMelementName,reservation | out-file c:\Output.txt
    
    powershell.exe-f myscript.ps1

    编辑:根据您的评论,这两个都不起作用。您需要使用PSRemoting在远程系统上从您的系统执行命令

    invoke-command -computername hypervhost -scriptblock {get-vmmemory | select VMelementName,reservation
    

    以上内容未经测试。如果这不起作用,您需要在您的环境中设置PSRemoting。

    通过使用UNC路径,您将从本地计算机上的远程计算机上运行PowerShell可执行文件。但是,这不会给您带来任何好处,因为这与运行安装在本地计算机上的PowerShell可执行文件没有什么不同你不能像那样加载安装在远程计算机上的模块

    您需要做的是在本地copmuter上安装,导入模块(如所述),然后对远程主机使用cmdlet:

    Get-VMMemory -VM "virtual_host" -Server "hyper-v_host"
    

    或者,您可以移动到。

    通过使用UNC路径,您将从本地计算机上的远程计算机运行PowerShell可执行文件。但是,这不会给您带来任何好处,因为这与运行安装在本地计算机上的PowerShell可执行文件没有什么不同。您将无法加载安装在远程计算机上的模块就这样

    您需要做的是在本地copmuter上安装,导入模块(如所述),然后对远程主机使用cmdlet:

    Get-VMMemory -VM "virtual_host" -Server "hyper-v_host"
    

    或者,您可以移动到。

    尝试以下操作:
    start”““c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe”“…
    尝试以下操作:
    start”““c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe”..
    我正在使用UNC,因为此脚本将从任何管理员的计算机上运行。UNC指向任何HV服务器上的PowerShell位置。我还尝试了以下操作:\\\c$\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe-命令“set executionpolicy RemoteSigned”还有:\\\c$\Windows\System32\WindowsPowerShell\v1.0\powershell.exe-命令“导入模块”\\SECAABHV03\c$\program files\modules\hyperv\hyperv.psd1';“获取vmmemory”>>aa.txt最后一个返回以下消息:Get WmiObject:Invalid namespace At\\\c$\program files\modules\hyperv\VM.ps1:73 char:26+Get WmiObject我检查了版本,似乎正在使用