首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cmdlet未识别

Cmdlet未识别
EN

Stack Overflow用户
提问于 2015-03-22 11:01:44
回答 1查看 22.3K关注 0票数 1

我写了我的第一个cmdlet。

\脚本\Modules\NewShare\New-Share.psm1

当我将.\Modules添加到$env:PSModulePath并导入模块时,就可以检索帮助,但是我无法执行它。

C:\Users\bp\Documents\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules\;.\Modules;= $evn:psmodulepath

当我运行脚本时

代码语言:javascript
复制
New-Share -foldername 'C:\MyShare' -sharename 'MyShare'

我得到以下标准错误,因为模块不存在。

术语“新共享”不被识别为cmdlet、函数、脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后再试一次。一行:1字符:10+新股<<<< -foldername 'C:\MyShare‘-sharename 'MyShare’+ CategoryInfo : ObjectNotFound:(新股票:String) [],CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

有什么问题吗?

下面是我的powershell模块。

代码语言:javascript
复制
function New-Share
{ 
    <#
        .Synopsis
        This function create a new share
        .Description
        This function creates a new share. If the specified folder does not exist, it will be created, and then shared with the specified share name.
        .Example
        New-Share -foldername 'C:\MyShare' -sharename 'MyShare'
        Creates the share with name 'MyShare' for folder 'C:\MyShare'.
        .Parameter foldername
        The folder that needs to be shared. Will be created if it does not exist.
        .Parameter sharename
        The name for the share.
        #Requires PowerShell 2.0
    #>
    [CmdletBinding()] 
     Param (
        [Parameter(Position=0, Mandatory=$True)] 
        [alias("fn")] 
        [string]$foldername, 
        [Parameter(Position=1, Mandatory=$True)] 
        [alias("sn")]
        [string]$sharename
        ) 

    if (!(test-path $foldername)) 
    { 
        new-item $foldername -type Directory 
    } 

    if (!(get-wmiObject Win32_Share -filter “name='$sharename'”)) 
    { 
        $shares = [WMICLASS]”WIN32_Share”

        if ($shares.Create($foldername, $sharename, 0).ReturnValue -ne 0) 
        {
            throw "Failed to create file share '$sharename'"
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-22 11:18:41

如果您从互联网上下载了该模块,则应该将其放在C:\Windows\System32\WindowsPowerShell\v1.0\Modules中。

  1. 你应该能够在你的会话中找到它。 Get模块-ListAvailable
  2. 然后您可以添加模块。 导入-模块新共享
  3. 验证cmdlet是否可用 获取命令-Module新共享

发生错误是因为您没有添加模块。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29193547

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档