简体   繁体   中英

get-childitem variable return empty value

I'm trying to figure out why, my Get-ChilddItem variable is returning an empty $null value while running the below scriptblock, whereas from ISE, if i enter variables values manually, i'm able to get an output of the .sql files I've selected before the Get-ChildItem command. Below is a beginning of the function i'm writing:

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}}

Thus, the replace operation cannot take place, as it loops on nothing. Am I writing this the wrong way for the intended purpose? Would you point me out to the right direction? Thanks!

Figured out my problem from this Stackoverflow post PowerShell Script to Find and Replace for all Files with a Specific Extension . I was pretty sure that the files property in the Get-Content command included Fullname or Name as member. Turns out that when I checked for the members i could not get any properties match FullName or Name:

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;}

So the only properties that relates to FullName property was PSPath as suggested in the post that i quoted above. Defining the right properties turns out to help replace the string inside each files as i was expecting to do. Credits to Daniel Liuzzi and Robben_Ford_Fan_boy for the hints.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM