前段时间我正在阅读PowerShell中的多变量赋值.这可以让你做这样的事情
64 > $a,$b,$c,$d = "A four word string".split()
65 > $a
A
66 > $b
four
Run Code Online (Sandbox Code Playgroud)
或者您可以在单个语句中交换变量
$a,$b = $b,$a
Run Code Online (Sandbox Code Playgroud)
你认为你所知道的那些鲜为人知的PowerShell小块可能并不像它们应该的那样众所周知?
我有一些使用COM API的PowerShell代码.传入字节数组时,我收到类型不匹配错误.这是我创建数组的方式,以及一些类型信息
PS C:\> $bytes = Get-Content $file -Encoding byte
PS C:\> $bytes.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
PS C:\> $bytes[0].GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Byte System.ValueType
Run Code Online (Sandbox Code Playgroud)
使用API,我发现它正在寻找一个基本类型为System.Array的Byte [].
PS C:\> $r.data.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Byte[] System.Array
PS C:\> $r.data[0].gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Byte System.ValueType
Run Code Online (Sandbox Code Playgroud)
我想要做的是将$ bytes转换为与$ r.data相同的类型.由于某种原因,$ bytes被创建为Object [].如何将其转换为Byte []?
我正在编写一些使用MSDeploy API的PowerShell脚本.我可以使用加载程序集
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Deployment")
Run Code Online (Sandbox Code Playgroud)
该地点位于GAC:
PS > [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Deployment") | fl Location
Location : C:\Windows\assembly\GAC_MSIL\Microsoft.Web.Deployment\7.1.0.0__31bf3856ad364e35\Microsoft.Web.Deployment.dll
Run Code Online (Sandbox Code Playgroud)
但是,我无法使用Add-Type加载程序集.我收到一个错误,说无法找到程序集,并且缺少一个或多个程序集.
PS > Add-Type -AssemblyName Microsoft.Web.Deployment
Add-Type : Cannot add type. The assembly 'Microsoft.Web.Deployment' could not be found.
At line:1 char:9
+ Add-Type <<<< -AssemblyName Microsoft.Web.Deployment
+ CategoryInfo : ObjectNotFound: (Microsoft.Web.Deployment:String) [Add-Type], Exception
+ FullyQualifiedErrorId : ASSEMBLY_NOT_FOUND,Microsoft.PowerShell.Commands.AddTypeCommand
Add-Type : Cannot add type. One or more required assemblies are missing.
At line:1 char:9
+ Add-Type <<<< -AssemblyName Microsoft.Web.Deployment
+ CategoryInfo : InvalidData: (:) …Run Code Online (Sandbox Code Playgroud) 我正在创建一个模块,该模块依赖于我需要加载到全局环境中的其他几个模块.我尝试创建一个脚本并使用ScriptsToProcess导入模块,但看起来在ScriptstoProcess运行之前检查RequiredModules.
在模块清单中是否有一个很好的,干净的方法来同时需要一个模块并在它尚未加载时自动加载它?如果无法加载模块,则RequiredModule将抛出错误.
我正在考虑动态构建一堆高级功能。为此,我一直在使用New-PSScript,但它无法实现我正在寻找的所有灵活性。
我正在阅读有关函数高级参数的手册页,并在帮助文章的末尾看到了有关动态参数的内容,其中提供了以下示例代码
function Sample {
Param ([String]$Name, [String]$Path)
DynamicParam
{
if ($path -match "*HKLM*:")
{
$dynParam1 = new-object
System.Management.Automation.RuntimeDefinedParameter("dp1",
[Int32], $attributeCollection)
$attributes = new-object System.Management.Automation.ParameterAttribute
$attributes.ParameterSetName = 'pset1'
$attributes.Mandatory = $false
$attributeCollection = new-object
-Type System.Collections.ObjectModel.Collection``1[System.Attribute]
$attributeCollection.Add($attributes)
$paramDictionary = new-object
System.Management.Automation.RuntimeDefinedParameterDictionary
$paramDictionary.Add("dp1", $dynParam1)
return $paramDictionary
} End if
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否可以使用 RuntimeDefinedParameter 和属性集合来生成新函数。
一些半伪代码可能看起来像这样。我(认为)正在尝试构建的两个关键函数是 New-Parameter 和 Add-Parameter。
$attributes1 = @{Mandatory=$true;Position=0;ValueFromPipeline=$true}
$param1 = New-Paramater -name foo -attributes $attributes1
$attributes2 = @{Mandatory=$true;Position=1}
$param2 = New-Paramater -name bar -attributes …Run Code Online (Sandbox Code Playgroud) 有许多工具可以以表格格式输出数据。一个这样的例子是diskpart。去掉一些无关的输出,你会得到这样的东西。
Disk ### Status Size Free Dyn Gpt
-------- ------------- ------- ------- --- ---
Disk 0 Online 136 GB 0 B
Disk 1 Offline 136 GB 136 GB
Disk 2 Reserved 1027 MB 0 B *
Disk 3 Reserved 500 GB 0 B *
Disk 4 Reserved 500 GB 0 B *
Disk 5 Reserved 10 GB 0 B *
Disk 6 Reserved 13 GB 0 B *
Disk 7 Reserved 4102 MB 0 B *
Disk 8 Reserved 7169 …Run Code Online (Sandbox Code Playgroud) 我有一堆字符串,是来自AD的DN组.我需要拔出Common Name.示例字符串是"CN =我想要的组名,OU =组容器,DC = corp,DC =测试,DC =本地"
我正在寻找的是一些PowerShell代码,它将从该字符串中拉出"我想要的组名"并丢弃其余部分.
我可以用这个来扯掉CN
$s = "CN=Group Name I Want,OU=Group Container,DC=corp,DC=test,DC=local"
$s = $s.Remove(0,3)
Run Code Online (Sandbox Code Playgroud)
但在那之后,我没有一个很好的方法从"OU"开始扯掉everthing
我相信有一些正则表达式可以做到这一点,但我需要一些帮助搞清楚.
为了上下文,我正在从已导入数据库的多个Excel工作表中进行一次数据转换.数据未规范化,我正在尝试将其标准化.
我在SQL数据库中有3个表.它们被称为OldAssets,OldTransactions和OldUsers.
OldTransactions有7903条记录.OldUsers有7437. OldAssets有9764
我正在使用LINQ to SQL来使用此代码查询这些表
from oa in OldAssets
from ot in OldTransactions
from u in OldUsers
where (oa.Asset_Serial_Number == ot.Asset_Serial_Number && ot.User_EID == u.User_EID && ot.Asset_Tag == oa.Asset_Tag)
select new Transactions {
DevCenter = ot.Transaction_Dev_Center,
Action = ot.Transaction_Action,
Status = ot.Transaction_Status,
ModificationDate = ot.Modified,
ModifiedBy = ot.ModifiedBy,
CreatedBy = ot.CreatedBy,
TransactionDate = (System.DateTime)ot.Transaction_Date,
Transaction_Asset = (System.Int32)oa.ID,
Transaction_User = (System.Int32)u.ID }
Run Code Online (Sandbox Code Playgroud)
我正在尝试遍历所有OldTransactions,并为每个OldTransaction创建一个新事务,该事务根据资产序列号和user_EID的映射指定Transaction_Asset和Transaction_User.
我的结果是给了我超过10,000条新的"交易记录".我不明白我怎么能获得比我拥有的OldTransactions数量更多的记录.
我可以编写哪些查询将为每个OldTransaction返回一个新的Transaction,但是根据serial_number和User_EID的映射添加了Transaction_Asset和Transaction_User的属性?
我试图让这个log4net自定义与PowerShell PSObjects一起使用.基本上,此模式布局将遍历对象并将其属性转储到CSV列中.以下是执行反射的代码:
PropertyInfo[] properties = loggingEvent.MessageObject.GetType().GetProperties();
foreach (PropertyInfo prop in properties)
{
object value = prop.GetValue(loggingEvent.MessageObject, null);
loggingEvent.Properties[prop.Name] = value;
}
Run Code Online (Sandbox Code Playgroud)
这在C#中使用普通类时工作正常.如果我在PowerShell中使用内联C#创建一个类,它也可以工作.
$code = @”
using System;
namespace CsvTest {
public class MyEvent
{
public string UserId { get; set; }
public string Details { get; set; }
public int EventCode { get; set; }
}
}
“@
Add-Type $code
$test = New-Object CsvTest.MyEvent
$test.UserId = "Andy"
$test.Details ="SomeStuff"
$test.EventCode = 10
$MyCommonLogger.Error($test)
$MyCommonLogger.Info($test)
$MyCommonLogger.Warn($test)
Run Code Online (Sandbox Code Playgroud)
如果我创建一个自定义PSObject,那么什么是行不通的.如果我用PSCustom对象替换$代码,如
$obj = …Run Code Online (Sandbox Code Playgroud) powershell ×9
regex ×2
arrays ×1
byte ×1
c# ×1
ldap ×1
linq-to-sql ×1
log4net ×1
msdeploy ×1
psobject ×1
reflection ×1
text-parsing ×1