Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Powershell返回从c运行相同脚本的不同结果#_C#_Powershell_Shell_File_Attributes - Fatal编程技术网

C# Powershell返回从c运行相同脚本的不同结果#

C# Powershell返回从c运行相同脚本的不同结果#,c#,powershell,shell,file,attributes,C#,Powershell,Shell,File,Attributes,我正在尝试运行一个简单的脚本来获取文件属性 string script = @"$path = 'C:\Temp\Indexing\Asm1.asm' $shell = New-Object -COMObject Shell.Application $folder = Split-Path $path $file = Split-

我正在尝试运行一个简单的脚本来获取文件属性

string script = @"$path = 'C:\Temp\Indexing\Asm1.asm'
                            $shell = New-Object -COMObject Shell.Application
                            $folder = Split-Path $path
                            $file = Split-Path $path -Leaf
                            $shellfolder = $shell.Namespace($folder)
                            $shellfile = $shellfolder.ParseName($file)                           
                            0..500 | Foreach-Object { '{0} = {1}' -f $shellfolder.GetDetailsOf($null, $_), $shellfolder.GetDetailsOf($shellfile, $_).toString()}";


Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(script);
pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
string script=@“$path='C:\Temp\index\Asm1.asm”
$shell=新对象-COMObject shell.Application
$folder=拆分路径$Path
$file=拆分路径$Path-Leaf
$shellfolder=$shell.Namespace($folder)
$shellfile=$shellfolder.ParseName($file)
0..500 | Foreach对象{{{0}={1}'-f$shellfolder.GetDetailsOf($null,$\),$shellfolder.GetDetailsOf($shellfile,$\).toString();
Runspace Runspace=RunspaceFactory.CreateRunspace();
Open();
Pipeline Pipeline=runspace.CreatePipeline();
pipeline.Commands.AddScript(脚本);
pipeline.Commands.Add(“输出字符串”);
收集结果=pipeline.Invoke();
runspace.Close();
在powershell中运行时,我会得到更多属性结果,而在代码中运行时这些结果是空的。 这似乎是一个访问问题。
任何帮助都将不胜感激

尝试添加
#/usr/bin/env pwsh
作为脚本的第一行。

我唯一的猜测是将
$env:UserName
作为脚本运行的用户进行检查。。也许这会把你引向正确的方向。。