get-content:无法找到路径' PATH_TO_FILE _'因为它不存在

时间:2018-01-03 18:47:23

标签: powershell

我正在编写PS脚本以尝试在远程服务器上找到一个文件夹。这是代码:

# Set file to be tested for, put everything after c:\
# “c:\Users\Default” is the example path
$filetofind = "EventLogs"
# Hostnames TXT Location
$hostnamestxt = "C:\Scripts\Powershell_Remote_File_Query\hosts.txt"
# Destination file for Online Machines
$onlinetxt = "C:\Scripts\Powershell_Remote_File_Query\Machines_with_file.txt"
# Destination file for Offline Machines
$offlinetxt = "C:\Scripts\Powershell_Remote_File_Query\Offline_Machines.txt"

##########################################################
# Begin Executing Script – Do Not Edit Below This Line
##########################################################
$computers = get-content “$hostnamestxt”

write-host “———————————————-”
write-host “Scanning hostnames from $hostnamestxt…”
write-host “———————————————-”

foreach($computer in $computers)
{
ping -n 1 $computer >$null
if($lastexitcode -eq 0)
{
if(test-path “\\$computer\c$\$filetofind”)
{
echo “$computer” | Out-File -Append “$onlinetxt”
write-host “File FOUND on $computer”
}
else
{write-host “File NOT found on $computer”}
}
else
{
echo “$computer” | Out-File -Append “$offlinetxt”
write-host “$computer is OFFLINE/DID NOT RESPOND TO PING”
}
}
write-host “———————————————-”
write-host “Script has completed please check output.”
write-host “Hosts with file output location – $onlinetxt”
write-host “Hosts that were unpingable output location – $offlinetxt”
write-host “———————————————-“

运行脚本时出现此错误:

get-content : Cannot find path 'C:\Scripts\Powershell_Remote_File_Query\“€' because it does not exist.
At C:\Scripts\Powershell_Remote_File_Query\Powershell_Remote_File_Query_Script.ps1:24 char:18
+     $computers = get-content “$hostnamestxtâ€
+                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (C:\Scripts\Powe...ile_Query\“€:String) [Get-Content], ItemNotFoundEx
   ception
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

脚本本身和txt文件都在同一目录中。请原谅我,我是Powershell的新手,而不是程序员。

1 个答案:

答案 0 :(得分:0)

而不是这样做......

<ContentPage.Content>
    <StackLayout VerticalOptions="FillAndExpand" Margin="0,30,0,0" HorizontalOptions="FillAndExpand">
        <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
            <Label Text="Branch" FontSize="11" TextColor="#0A7475" HorizontalOptions="Start" Margin="10,0,0,0" />
            <Label Text="Restock Reason" TextColor="#0A7475" FontSize="11" HorizontalOptions="Start" Margin="20,0,0,0" />
            <Label Text="Qty" TextColor="#0A7475" FontSize="11" HorizontalOptions="Start" Margin="20,0,0,0" />
            <Label Text="Vendor" TextColor="#0A7475" FontSize="11" HorizontalOptions="Start" Margin="20,0,0,0" />
            <Label Text="Created On" TextColor="#0A7475" FontSize="11" HorizontalOptions="Start" Margin="20,0,0,0" />
        </StackLayout>
        <ListView x:Name="listViewn" BackgroundColor="White">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal" HorizontalOptions="StartAndExpand">
                            <Label x:Name="Branchh" TextColor="Gray" FontSize="11" HorizontalOptions="Start" Text="{Binding Branch}" Margin="10,5,0,10" />
                            <Label x:Name="reason" TextColor="#325772" FontSize="11" Text="{Binding Reason}" Margin="20,5,0,10" />
                            <Label x:Name="Qtty" TextColor="Gray" FontSize="11" HorizontalOptions="Center" Text="{Binding Qty}" Margin="0,5,0,0" />
                            <Label x:Name="vendor" TextColor="Gray" FontSize="11" Text="{Binding Vendor}" Margin="20,5,0,10" />
                            <Label x:Name="schedDate" TextColor="Gray" HorizontalOptions="End" FontSize="11" Text="{Binding SchedDate}" Margin="0,5,10,10" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage.Content>

试着这样做......

$hostnamestxt = "C:\Scripts\Powershell_Remote_File_Query\hosts.txt"
$computers = get-content “$hostnamestxt”

我明白你要做的事情应该导致同样的事情,但是,让我们试着消除那些额外的事情,并试一试,看看你是否得到了不同的结果。

此外,如果您正在阅读的文件位于远程主机上,那么您应该使用UNC文件共享路径,而不是本地C:驱动器。只是假设一点。