簡體   English   中英

Get-ChildItem-獲取當前目錄的路徑

[英]Get-ChildItem -Path Getting Current Directory

我有一個簡單的任務,即從一個路徑獲取文件,然后使用PowerShell腳本將其復制到另一個路徑。 現在,我知道使用Get-ChildItem ,如果未指定-Path ,它將獲取當前目錄。 我正在使用此代碼,設置文件所在的目錄,但仍在獲取當前目錄:

$file = Get-ChildItem -Path "\\w102xnk172\c$\inetpub\wwwroot\PC_REPORTS\exemplo\DCT\Files\STC" | Sort-Object LastWriteTime | Select-Object -Last 1
Copy-Item $file -Destination "\\Brhnx3kfs01.vcp.amer.dell.com\brh_shipping$\DEMAND_MONITOR\"
cmd /c pause | out-null

暫停時返回此錯誤:

在此處輸入圖片說明

這是腳本,遵循Get-ChildItemCopy-Item的文檔中的規則。 我想念什么嗎? 為什么仍然獲取當前目錄? 我嘗試了多種語法差異,使路徑脫離逗號,而不是設置-Path或-Destination,直接在Copy-Item中設置文件而不使用$file變量...

Copy-Item需要一個[string]作為第一個位置參數-所以嘗試轉換$file為一個字符串,這會導致文件的名稱

引用文件的FullName屬性值(完整路徑):

Copy-Item $file.FullName -Destination "\\Brhnx3kfs01.vcp.amer.dell.com\brh_shipping$\DEMAND_MONITOR\"

或通過管道將$file對象傳遞給Copy-Item然后讓管道綁定為您完成它的魔力:

$file |Copy-Item -Destination "\\Brhnx3kfs01.vcp.amer.dell.com\brh_shipping$\DEMAND_MONITOR\"

如果您想親自了解powershell在內部如何處理此消息,請使用Trace-Command

Trace-Command -Name ParameterBinding -Expression {Copy-Item $file -Destination "\\Brhnx3kfs01.vcp.amer.dell.com\brh_shipping$\DEMAND_MONITOR\"} -PSHost

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM