我正在学习一些PowerShell.是否可以看到像Get-ChildItem这样的内置cmdlet的源代码?
我刚刚在PowerShell v1.0上测试了这个.设置如下:
Id CommandLine
-- -----------
1 $msbuild = "C:\Windows\Microsoft.NET\Framework\v3.5\msbuild.exe"
4 $a = "C:\some\project\or\other\src\Solution.sln /target:Clean /target:Build"
Run Code Online (Sandbox Code Playgroud)
.
此行失败,并显示不直观的错误消息:
Id CommandLine
-- -----------
5 & $msbuild $a
Run Code Online (Sandbox Code Playgroud)
.
此行失败,因为&期望第一个参数是命令本身.
Id CommandLine
-- -----------
10 & "$msbuild $a"
Run Code Online (Sandbox Code Playgroud)
.
这条线有效:
Id CommandLine
-- -----------
16 cmd /c "$msbuild $a"
Run Code Online (Sandbox Code Playgroud)
.
请解释.我更感兴趣的是&语法不起作用,而不是MSBuild特定的解决方法.
我希望能够在没有任何列标题的情况下从PowerShell输出数据.我知道我可以隐藏列标题使用Format-Table -HideTableHeaders,但在顶部留下一个空行.
这是我的例子:
get-qadgroupmember 'Domain Admins' | Select Name | ft -hide | out-file Admins.txt
Run Code Online (Sandbox Code Playgroud)
如何消除列标题和空白行?
我可以添加另一行并执行此操作:
Get-Content Admins.txt | Where {$_ -ne ""} | out-file Admins1.txt
Run Code Online (Sandbox Code Playgroud)
但我能在一条线上做到这一点吗?
我正在使用Powershell编写构建脚本.这些脚本执行各种操作,例如从SVN获取最新的源代码,备份等,并使用MSBuild构建解决方案:
cmd /c C:\WINDOWS\Microsoft.NET\Framework\v3.5\msbuild.exe "C:\Dev\Path\MyProjct.sln" /p:Configuration=Release
Run Code Online (Sandbox Code Playgroud)
在这条指令之后,如果编译成功,我只想执行其余的脚本.我怎么检查这个?
该项目是一个Web项目,因此检查输出并不容易,但我猜一些变量将包含编译结果.此外,因为我用cmd/c调用msbuild,我能够访问这些变量吗?
一些C#代码使用参数执行powershell脚本.我想从Powershell获取一个返回码和一个字符串,以便知道如果Powershell脚本中的所有内容都正常.
什么是正确的方法 - 在Powershell和C#中
# Powershell script
# --- Do stuff here ---
# Return an int and a string - how?
# In c# I would do something like this, if this was a method:
# class ReturnInfo
# {
# public int ReturnCode;
# public string ReturnText;
# }
# return new ReturnInfo(){ReturnCode =1, ReturnText = "whatever"};
Run Code Online (Sandbox Code Playgroud)
void RunPowershellScript(string scriptFile, List<string> parameters)
{
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
{
runspace.Open();
RunspaceInvoke scriptInvoker …Run Code Online (Sandbox Code Playgroud) 将哈希表传递给我的PowerShell函数时,它会抱怨它收到一个对象.
Function ExtendHash(){
param(
[hashtable] $source,
[hashtable] $extender
)
...
}
Run Code Online (Sandbox Code Playgroud)
和来电者:
$hash1 = @{One = 1; Two = 2}
$hash2 = @{Two = 22; three = 3}
ExtendHash($hash1, $hash2)
Run Code Online (Sandbox Code Playgroud)
无法将System.Object []类型的System.Object []值转换为System.Collection.Hashtable类型
那我该怎么做呢?建议?
另外,我错过了内置的东西吗?我想要与JavaScript用于扩展默认选项(合并和覆盖默认值)相同的模式.
$result = Get-ADUser -Filter $filter
Run Code Online (Sandbox Code Playgroud)
如果我有2个或更多结果,我得到$ x作为数组,但如果我只有一个结果,则得到$ x作为对象.如何使它更正确,总是收到数组 - 空,一个元素或一些元素?
我正在寻找一种从命令提示符运行几个PowerShell命令的方法.我不想为此创建脚本,因为它只需要运行几个命令,因为我真的不知道如何使用PowerShell编写脚本.
这是我试图用来开始的命令:
Get-AppLockerFileInformation -Directory <folderpath> -Recurse -FileType <type>
Run Code Online (Sandbox Code Playgroud)
我真的不想为此创建一个脚本,因为如果我可以从批处理文件中运行一个或两个命令与其余的东西会更容易.
编辑:这是我到目前为止所尝试的.
1)
powershell -Command "Get-AppLockerFileInformation....."
Error: The term 'Get-AppLockerFileInformation is not recognized as the name of a cmdlet, function, script file, or operable program....
Run Code Online (Sandbox Code Playgroud)
2)
powershell -Command {Get-AppLockerFileInformation.....}
Run Code Online (Sandbox Code Playgroud)
这种方式没有错误,但我没有得到任何回报.如果我使用Set-AppLockerPolicy...没有任何反应.
3)
powershell -Command "{Get-AppLockerFileInformation.....}"
Error: The term 'Get-AppLockerFileInformation is not recognized as the name of a cmdlet, function, script file, or operable program....
Run Code Online (Sandbox Code Playgroud)
4)
powershell -Command "& {Get-AppLockerFileInformation.....}"
Error: The term 'Get-AppLockerFileInformation is not recognized as the name of a …Run Code Online (Sandbox Code Playgroud) 我编写了这个PowerShell脚本,该脚本应该在特定日期范围内存档所有日志文件.
$currentDate = Get-Date;
$currentDate | Get-Member -Membertype Method Add;
$daysBefore = -1;
$archiveTillDate = $currentDate.AddDays($daysBefore);
$sourcePath = 'C:\LOGS';
$destPath='C:\LogArchieve\_'+$archiveTillDate.Day+$archiveTillDate.Month+$archiveTillDate.Year+'.zip';
foreach( $item in (Get-ChildItem $sourcePath | Where-Object { $_.CreationTime -le $archiveTillDate }) )
{
[Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem");
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal;
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcePath,$destPath, $compressionLevel, $false);
}
Run Code Online (Sandbox Code Playgroud)
它一直工作到foreach循环之前,但是在循环中它会产生以下错误:
Unable to find type [System.IO.Compression.CompressionLevel]: make sure that the assembly containing this type is loaded.
At line:4 char:65
+ $compressionLevel = [System.IO.Compression.CompressionLevel] <<<< ::Optimal;
+ CategoryInfo : InvalidOperation: (System.IO.Compression.CompressionLevel:String) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound …Run Code Online (Sandbox Code Playgroud) compression powershell runtime-error powershell-2.0 .net-assembly
我已经在网上看了一会儿,发现了许多类似的问题,但由于某种原因,我似乎无法让这个工作.
我只是想连接到SQL服务器数据库并将查询结果输出到文件 - 请参阅下面的PowerShell脚本.我不确定的是如何将用户ID和密码集成到连接字符串中.
$SQLServer = "aaaa.database.windows.net"
$SQLDBName = "Database"
$uid ="john"
$pwd = "pwd123"
$SqlQuery = "SELECT * from table;"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True; User ID = $uid; Password = $pwd;"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$DataSet.Tables[0] | out-file "C:\Scripts\xxxx.csv"
Run Code Online (Sandbox Code Playgroud)
收到以下错误消息:
使用"1"参数调用"Fill"的异常:"此版本的SQL Server不支持Windows登录."
powershell ×10
batch-file ×1
c# ×1
cmd ×1
cmdlets ×1
compression ×1
hashtable ×1
msbuild ×1
sql-server ×1
windows ×1