变量在循环中消失?电源外壳

时间:2014-07-03 15:02:08

标签: powershell

以下代码有效。想象一下,在传递之前,我的主脚本中的文件正在读取变量$ Datfilecontents。该脚本应该在UNC中尝试数组的每个部分,如此

\\ 1404397769 \ LDMS_MASTER_SHARE \\ SAO-LDMS \ LDMS_MASTER_SHARE

在下面的脚本中,每个循环都会看到1404397769和SAO-LDMS并尝试两者。

在从.dat文件读取$ DatFileContents的主脚本中(值与之前的回声相同)问题是在循环中这是发生的事情..

\\ 1404397769 \ LDMS_MASTER_SHARE 首先得到它应该传递.... 然后 \\ SAO-LDMS

为什么$ NetworkPath的最后一部分在循环中丢失了?

我正在处理的主脚本的唯一区别是$ Datfilecontents是从文件读取的,只要数据是相同的,它应该工作?我不知道数组中偶数不变的变量是如何丢失的?

我在这里遗漏了什么吗?任何帮助表示赞赏。

$Datfilecontents = "1404397769;SAO-LDMS"
echo $1 "Before the split"
$1 = $DatFileContents.split(";");
echo $1 "after the split"
[array]::Reverse($1)
echo $1 "after the reverse"
Do
{
Foreach ($i in $1)
{
    [String]$i


    $net = new-object -ComObject WScript.Network
    [String]$NetworkPath = "\\$i\LDMS_MASTER_SHARE"


    $NetworkPath
    Write-Host $NetworkPath "THIS IS THE PATH"

    Try
    {
        $net.MapNetworkDrive("M:", $NetworkPath, $false, "domain\user", "password")
    echo "Mapping Drive M to Preferred Server"}
    Catch
    {echo "Failed to Map Drive M to Preferred Server" 





}

}
until (Test-Path "M:\LDMS BUILD")


This is the output from the main script.

\\\1404398737\LDMS_MASTER_SHARE THIS IS THE PATH
Failed to Map Drive M to Preferred Server
\\\SAO-LDMS THIS IS THE PATH
Failed to Map Drive M to Preferred Server

正如您所看到的,第二遍未显示分享?所以如果无法映射驱动器。但是,共享变量必须是有效的,因为它在第一次传递后才会有效吗?

The full output from the code above.
Before the split
1404397769
SAO-LDMS
after the split
SAO-LDMS
1404397769
after the reverse
SAO-LDMS
\\SAO-LDMS\LDMS_MASTER_SHARE
\\SAO-LDMS\LDMS_MASTER_SHARE THIS IS THE PATH
Mapping Drive M to Preferred Server
1404397769
\\1404397769\LDMS_MASTER_SHARE
\\1404397769\LDMS_MASTER_SHARE THIS IS THE PATH
Failed to Map Drive M to Preferred Server

主脚本的完整输出。您可以在尝试传递值之前看到值存在。但它从第一次传递中删除了份额?

Before the split
1404401722
SAO-LDMS
after the split 
SAO-LDMS
1404401722
after the reverse
SAO-LDMS
\\SAO-LDMS
\\SAO-LDMS THIS IS THE PATH
Failed to Map Drive M to Preferred Server
1404401722
\\1404401722\LDMS_MASTER_SHARE
\\1404401722\LDMS_MASTER_SHARE THIS IS THE PATH
Failed to Map Drive M to Preferred Server

好这是主要的脚本 - 我真的看不出有什么区别。

$DatFileLoc = "c:\Program Files (x86)\LANDesk\LDClient\sdmcache\$DatFile";
    Write-Host $DatFileLoc
    $DatFileLoc
    Write-Host "Preferred Server File found at $DatFileLoc";
    Write-Host "Getting File Contents";
    $DatFileContents = Get-Content $DatFileLoc
    Write-Host $DatFileContents

$DatFileContents = $DatFileContents -replace "\?", ";";
Write-Host $DatFileContents
echo $1 "Before the split"
$1 = $DatFileContents.split(";");
echo $1 "after the split"       

  [array]::Reverse($1)
echo $1 "after the reverse"

Do
{
Foreach ($i in $1)
{
    [String]$i

    $net = new-object -ComObject WScript.Network
    [String]$NetworkPath = "\\$i\LDMS_MASTER_SHARE"


    $NetworkPath
    Write-Host $NetworkPath "THIS IS THE PATH"

    Try
    {
        $net.MapNetworkDrive("M:", $NetworkPath, $false, "domain\user", "password")
    echo "Mapping Drive M to Preferred Server"}
    Catch
    {echo "Failed to Map Drive M to Preferred Server" }

}

}
until (Test-Path "M:\LDMS BUILD")

那个输出......

c:\Program Files (x86)\LANDesk\LDClient\sdmcache\preferredservers.dat
Preferred Server File found at c:\Program Files    (x86)\LANDesk\LDClient\sdmcache\preferredservers.dat
Getting File Contents
1404403455?SAO-LDMS
1404403455;SAO-LDMS
Before the split
1404403455
SAO-LDMS
after the split
SAO-LDMS
1404403455
after the reverse
SAO-LDMS
\\SAO-LDMS
\\SAO-LDMS THIS IS THE PATH
Failed to Map Drive M to Preferred Server
1404403455
\\1404403455\LDMS_MASTER_SHARE
\\1404403455\LDMS_MASTER_SHARE THIS IS THE PATH
Failed to Map Drive M to Preferred Server

1 个答案:

答案 0 :(得分:0)

我保存了您的文件,将路径更改为我的下载文件夹(为安全起见,用户名已更改)。这是我从ISE和控制台运行的代码:

$DatFileContents = Get-Content "C:\Users\TMTech\Downloads\preferredservers.txt";
$DatFileContents = $DatFileContents -replace "\?", ";";
echo $DatFileContents "Before the split"
$1 = $DatFileContents.split(";");
echo $1 "after the split"
[array]::Reverse($1)
echo $1 "after the reverse"
Do
{
    Foreach ($i in $1)
    {
        [String]$i

        $net = new-object -ComObject WScript.Network
        [String]$NetworkPath = "\\$i\LDMS_MASTER_SHARE"


        $NetworkPath
        Write-Host $NetworkPath "THIS IS THE PATH"

        Try
        {
            $net.MapNetworkDrive("M:", $NetworkPath, $false, "domain\user", "password")
        echo "Mapping Drive M to Preferred Server"}
        Catch
        { echo "Failed to Map Drive M to Preferred Server" }




    }

}
until (Test-Path "M:\LDMS BUILD")

这就是我跑的时候发生的事情:

1404418053;SAO-LDMS
Before the split
1404418053
SAO-LDMS
after the split
SAO-LDMS
1404418053
after the reverse
SAO-LDMS
\\SAO-LDMS \LDMS_MASTER_SHARE
\\SAO-LDMS \LDMS_MASTER_SHARE THIS IS THE PATH
Failed to Map Drive M to Preferred Server
1404418053
\\1404418053\LDMS_MASTER_SHARE
\\1404418053\LDMS_MASTER_SHARE THIS IS THE PATH
Failed to Map Drive M to Preferred Server
SAO-LDMS
\\SAO-LDMS \LDMS_MASTER_SHARE
\\SAO-LDMS \LDMS_MASTER_SHARE THIS IS THE PATH

一些注意事项:

    当您尝试在echo $1 "Before the split"中回显它时,
  • $ 1不存在您可能意味着回显$ datfilecontents。
  • PowerShell中不需要行末尾的分号。
  • 您不需要声明[String]$i[String]$NetworkPath,因为他们无论如何都会成为字符串。
  • 您可以取消其中的每个echo,默认情况下会将这些行发送到输出。
  • 这些都不会阻止脚本工作。
很遗憾,你没有20个声誉,我们可以将这个对话带入私人房间的Stack Overflow聊天服务器,而不是来回回复评论。