首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PowerShell未将新建项目对象引用设置为对象实例

PowerShell未将新建项目对象引用设置为对象实例
EN

Stack Overflow用户
提问于 2017-02-13 22:20:09
回答 1查看 11K关注 0票数 0

我正在用powershell编写一个工具,其中包含一个根据用户输入创建文件夹结构的函数。相关代码如下:

代码语言:javascript
运行
复制
function set-drvsource{
    Write-Host "INFO: Querying Driver Source" -ForegroundColor Yellow
    }
    try{
        Write-Host "INFO: Creating standard folder structure" -ForegroundColor Yellow
        New-Item -ItemType Directory -Path "$drvpath" | Out-Null
        New-Item -ItemType Directory -Path "$drvpath\Source" | Out-Null
        New-Item -ItemType Directory -Path "$drvpath\Package" | Out-Null
    }
    catch{
        Write-Host "ERROR: Could not create folder structure" -ForegroundColor Red
        Exit
    }
    Write-Host "INFO: Moving local source to new source" -ForegroundColor Yellow
    Copy-Item $src "$drvpath\Source" -Recurse
}
function main{
    set-drvsource
}
$drvroot = "\\server\share\tmp"
$src = Read-Host "Please provide local driver source path"
$vendor = Read-Host "Device Vendor eg. DELL, HP, Microsoft"
$dmd = Read-Host "Device Model eg. HP Elitebook 840 G3"
$dos = Read-Host "Operating System eg. W7 or W10"
$dac = Read-Host "x64 or x86"
$drvpath = "$drvroot\$vendor\$dmd $dos $dac"

如果我运行它,它会触发catch语句并抛出以下错误:

代码语言:javascript
运行
复制
$Error[0]
New-Item : Object reference not set to an instance of an object.
At line:9 char:1
+ New-Item -ItemType Directory -Path "$drvpath" | Out-Null
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [New-Item], NullReferenceException
    + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.NewI 
   temCommand

有趣的是,如果我在新的PowerShell会话中运行以下命令,它会按预期工作:

代码语言:javascript
运行
复制
$drvroot = "\\server\share\tmp"
$vendor = Read-Host "Vendor"
$dmd = Read-Host "Device Model eg. HP Elitebook 840 G3"
$dos = Read-Host "Operating System eg. W7 or W10"
$dac = Read-Host "x64 or x86"
$drvpath = "$drvroot\$vendor\$dmd $dos $dac"
New-Item -ItemType Directory -Path "$drvpath" | Out-Null
New-Item -ItemType Directory -Path "$drvpath\Source" | Out-Null
New-Item -ItemType Directory -Path "$drvpath\Package" | Out-Null

我已经检查了两次,$drvpath确实通过了函数,所以这不是一个作用域问题?任何帮助都将不胜感激!:'(

编辑:$ERROR | fl -force的输出

代码语言:javascript
运行
复制
Exception             : System.NullReferenceException: Object reference not 
                        set to an instance of an object.
                           at Microsoft.ConfigurationManagement.PowerShell.Prov
                        ider.CMDriveProvider.NewItem(String path, String 
                        itemTypeName, Object newItemValue)
                           at System.Management.Automation.SessionStateInternal
                        .NewItemPrivate(CmdletProvider providerInstance, 
                        String path, String type, Object content, 
                        CmdletProviderContext context)
TargetObject          : 
CategoryInfo          : NotSpecified: (:) [New-Item], NullReferenceException
FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Comm
                        ands.NewItemCommand
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at set-drvsource, C:\temp\quick
                        import_cli-1.1\quickimport_cli-1.1\Invoke-QuickImport.p
                        s1: line 51
                        at main, C:\temp\quickimport_cl
                        i-1.1\quickimport_cli-1.1\Invoke-QuickImport.ps1: line 
                        101
                        at <ScriptBlock>, C:\temp\quick
                        import_cli-1.1\quickimport_cli-1.1\Invoke-QuickImport.p
                        s1: line 165
PipelineIterationInfo : {}
PSMessageDetails      : 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-15 20:21:56

按照PetSerAl的建议,使用FileSystem提供程序定义$drvroot变量解决了问题。

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

https://stackoverflow.com/questions/42206283

复制
相关文章

相似问题

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