2017-06-24 40 views
0

我寫PowerShell腳本來創建新的文件後,但其投擲運行時錯誤:投擲的錯誤在新建項目在PowerShell中創建新文件

New-Item -path c:\subhashis -name $input2 -type "file" 

其中$input2是我傳遞

運行參數

[DBG] >>>在停止:$ _ PSParentPath.Replace( 「Microsoft.PowerShell.Core \文件系統::」, 「」)

+1

爲什麼你保持調試器?它在$ error中捕獲了什麼? –

+0

試試這個來創建一個空文件並查看結果'[System.IO.File] :: CreateText(「E:\ EmptyFile.txt」)'或者使用'New-Item C:\ subhashis \ new_file.txt -type文件強制價值「這是文字」' –

回答

0

它聽起來像有一個問題與您調用New-Item的方式有關。在語法只是說明:

New-Item -ItemType 'File' -Name 'String' -Path 'C:\Temp' 

根據您的錯誤代碼,我不能告訴你到底發生了什麼錯在你的代碼沒有看到更多。

或者,您可以忽略名稱標誌並使用-Path "C:\Temp\$filename"代替。

+0

我試圖取代你提到的語法,但得到相同的錯誤。這裏是我的代碼段函數test-param {[int] $ input1,[String] $ input2) $ chunk = 0 foreach($ line in $ fileinput1){ if($ line.substring(0, 2)-eq'Ab') { $ chunk ++; New-Item -Itemtype'File'-Path「c:\ subhashis \ $ input2」 } –

+0

@SubhashisKhan請勿在評論中發佈代碼,[請修改您的問題](https://stackoverflow.com/posts/44733062 /編輯)來包含這些附加信息,以便後續讀者無需閱讀所有評論。 – LotPings