2016-07-04 120 views
0

我想弄清楚爲什麼,我的Get-ChilddItem變量在運行下面的scriptblock時返回一個空的$ null值,而從ISE,如果我手動輸入變量值,我可以獲取我在Get-ChildItem命令之前選擇的.sql文件的輸出。以下是我正在寫的功能的開始:get-childitem變量返回空值

Function test{ 
[CmdletBinding()] 
Param (
    [Parameter(Mandatory=$True] 
    [string]$client 
    ) 
# Define variables 
$Master = 'master' 
$Slave = 'slave' 
$SourcePath = "C:\somefolder\folder" 
$Masterfolder = "C:\somemasterfolder" 
$Slavefolder = "C:\someslavefolder" 

# Create folders 
    $clientdir2 = New-Item -Path $Slavefolder -name "$client" -ItemType Directory 

# Create folder on secondary server 
    $clientdir = New-Item -Path "Microsoft.PowerShell.Core\FileSystem::\\$master\somemasterfolder" -name "$client" -ItemType Directory 

# Getting all sql files and copy them to the destination folder 
    $gflz = Get-ChildItem $SourcePath\* -Include __07*, __08*, __10* 
# Copy 
    Copy-Item $gflz $clientdir2 

# Replace values in each sql files 
    $cpz = @(Get-ChildItem $clientdir2\* -Include *.sql) 
if ($cpz -ne $null) {    
    Foreach ($flz in $cpz) { 
     (Get-Content $flz.FullName) | ForEach-Object { $_ -replace'clientID', "$client" -replace 'C:\LSpath', ` 
     "$Masterfolder" -replace'C:\LSPath2', "$Slavefolder" } | Set-Content $flz.FullName}} 

因此,替換操作不能發生,因爲它什麼都不會發生。 我是否按照預期目的寫錯了這個方法? 你能指出我走向正確的方向嗎? 謝謝!

回答

0

找出我的問題從這個Stackoverflow文章PowerShell Script to Find and Replace for all Files with a Specific Extension。 我很確定Get-Content命令中的文件屬性包括全名名稱作爲成員。原來,當我檢查的成員,我不能得到任何屬性匹配全名或名稱:

Get-Content $sqlfiles | gm -MemberType Properties 

    TypeName: System.String 

Name   MemberType Definition 
----   ---------- ---------- 
PSChildName NoteProperty System.String   
PSChildName=Mysqlfiles.sql 
PSDrive  NoteProperty System.Management.Automation.PSDriveInfo PSDrive=C 
PSParentPath NoteProperty System.String  
PSParentPath=C:\parentpath 
PSPath  NoteProperty System.String  
PSPath=mysqlfilefullpathname.sql 
PSProvider NoteProperty System.Management.Automation.ProviderInfo  
PSProvider=Microsoft.PowerShell.Core\FileSystem 
ReadCount NoteProperty System.Int64 ReadCount=1 
Length  Property  int Length {get;} 

所以,涉及到FullName屬性是PSPath在我報價後提出的唯一屬性以上。 定義正確的屬性證明可以幫助替換每個文件中的字符串,就像我期望的那樣。 積分爲Daniel LiuzziRobben_Ford_Fan_boy提示。