【问题标题】:get-childitem variable return empty valueget-childitem 变量返回空值
【发布时间】:2016-07-04 20:47:22
【问题描述】:

我试图找出原因,我的 Get-ChilddItem 变量在运行下面的脚本块时返回一个空的 $null 值,而在 ISE 中,如果我手动输入变量值,我能够得到一个输出我在Get-ChildItem 命令之前选择的 .sql 文件。下面是我正在编写的函数的开头:

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

因此,替换操作无法进行,因为它没有循环。 我是否出于预期目的以错误的方式写了这个? 你能指出我正确的方向吗? 谢谢!

【问题讨论】:

    标签: windows powershell powershell-3.0


    【解决方案1】:

    从这篇 Stackoverflow 帖子PowerShell Script to Find and Replace for all Files with a Specific Extension 中找出了我的问题。 我很确定 Get-Content 命令中的 files 属性包含 FullnameName 作为成员。事实证明,当我检查成员时,我无法获得与 FullName 或 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;}
    

    因此,与 FullName 属性相关的唯一属性是 PSPath,正如我在上面引用的帖子中所建议的那样。 正如我所期望的那样,定义正确的属性有助于替换每个文件中的字符串。 感谢 Daniel LiuzziRobben_Ford_Fan_boy 提供提示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多