【发布时间】:2018-03-20 03:56:32
【问题描述】:
PowerShell 新手。我正在创建一个包含用户信息的自定义对象列表,然后遍历该列表并将用户添加到 AD。我的脚本在 ISE 中完美运行,但我想将其设置为 Windows 中的计划任务。这样做时,我发现直接在 powershell.exe 中调用它不起作用。重要的位:
$usersToAdd = New-Object System.Collections.Generic.List[System.Object]
foreach($obj in $listofobjs)
{ $user = [PSCustomObject]@{
'param1' = "username"
'param2' = "group"
}
$usersToAdd.Add($user)
}
ForEach($user in $usersToAdd)
{
Add-ADGroupMember -Identity $user.param1 -Members $user.param2 -ErrorAction Stop
}
在命令行中,add cmdlet 失败,因为参数为空。回显 $user 变量给了我:
@{param1=stuff; param2=stuff2}
在 ISE 回显中它给出:
param1 : stuff
param2 : stuff2
编辑:全文回显 然后在命令行 $user |获取会员:
TypeName : System.String
Name : Clone
MemberType : Method
Definition : System.Object Clone(), System.Object ICloneable.Clone()
TypeName : System.String
Name : CompareTo
MemberType : Method
Definition : int CompareTo(System.Object value), int CompareTo(string strB), int IComparable.CompareTo(System.Object
obj), int IComparable[string].CompareTo(string other)
TypeName : System.String
Name : Contains
MemberType : Method
Definition : bool Contains(string value)
TypeName : System.String
Name : CopyTo
MemberType : Method
Definition : void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
TypeName : System.String
Name : EndsWith
MemberType : Method
Definition : bool EndsWith(string value), bool EndsWith(string value, System.StringComparison comparisonType), bool
EndsWith(string value, bool ignoreCase, cultureinfo culture)
TypeName : System.String
Name : Equals
MemberType : Method
Definition : bool Equals(System.Object obj), bool Equals(string value), bool Equals(string value,
System.StringComparison comparisonType), bool IEquatable[string].Equals(string other)
TypeName : System.String
Name : GetEnumerator
MemberType : Method
Definition : System.CharEnumerator GetEnumerator(), System.Collections.IEnumerator IEnumerable.GetEnumerator(),
System.Collections.Generic.IEnumerator[char] IEnumerable[char].GetEnumerator()
TypeName : System.String
Name : GetHashCode
MemberType : Method
Definition : int GetHashCode()
TypeName : System.String
Name : GetType
MemberType : Method
Definition : type GetType()
TypeName : System.String
Name : GetTypeCode
MemberType : Method
Definition : System.TypeCode GetTypeCode(), System.TypeCode IConvertible.GetTypeCode()
TypeName : System.String
Name : IndexOf
MemberType : Method
Definition : int IndexOf(char value), int IndexOf(char value, int startIndex), int IndexOf(char value, int startIndex,
int count), int IndexOf(string value), int IndexOf(string value, int startIndex), int IndexOf(string
value, int startIndex, int count), int IndexOf(string value, System.StringComparison comparisonType), int
IndexOf(string value, int startIndex, System.StringComparison comparisonType), int IndexOf(string value,
int startIndex, int count, System.StringComparison comparisonType)
TypeName : System.String
Name : IndexOfAny
MemberType : Method
Definition : int IndexOfAny(char[] anyOf), int IndexOfAny(char[] anyOf, int startIndex), int IndexOfAny(char[] anyOf,
int startIndex, int count)
TypeName : System.String
Name : Insert
MemberType : Method
Definition : string Insert(int startIndex, string value)
TypeName : System.String
Name : IsNormalized
MemberType : Method
Definition : bool IsNormalized(), bool IsNormalized(System.Text.NormalizationForm normalizationForm)
TypeName : System.String
Name : LastIndexOf
MemberType : Method
Definition : int LastIndexOf(char value), int LastIndexOf(char value, int startIndex), int LastIndexOf(char value, int
startIndex, int count), int LastIndexOf(string value), int LastIndexOf(string value, int startIndex), int
LastIndexOf(string value, int startIndex, int count), int LastIndexOf(string value,
System.StringComparison comparisonType), int LastIndexOf(string value, int startIndex,
System.StringComparison comparisonType), int LastIndexOf(string value, int startIndex, int count,
System.StringComparison comparisonType)
TypeName : System.String
Name : LastIndexOfAny
MemberType : Method
Definition : int LastIndexOfAny(char[] anyOf), int LastIndexOfAny(char[] anyOf, int startIndex), int
LastIndexOfAny(char[] anyOf, int startIndex, int count)
TypeName : System.String
Name : Normalize
MemberType : Method
Definition : string Normalize(), string Normalize(System.Text.NormalizationForm normalizationForm)
TypeName : System.String
Name : PadLeft
MemberType : Method
Definition : string PadLeft(int totalWidth), string PadLeft(int totalWidth, char paddingChar)
TypeName : System.String
Name : PadRight
MemberType : Method
Definition : string PadRight(int totalWidth), string PadRight(int totalWidth, char paddingChar)
TypeName : System.String
Name : Remove
MemberType : Method
Definition : string Remove(int startIndex, int count), string Remove(int startIndex)
TypeName : System.String
Name : Replace
MemberType : Method
Definition : string Replace(char oldChar, char newChar), string Replace(string oldValue, string newValue)
TypeName : System.String
Name : Split
MemberType : Method
Definition : string[] Split(Params char[] separator), string[] Split(char[] separator, int count), string[]
Split(char[] separator, System.StringSplitOptions options), string[] Split(char[] separator, int count,
System.StringSplitOptions options), string[] Split(string[] separator, System.StringSplitOptions
options), string[] Split(string[] separator, int count, System.StringSplitOptions options)
TypeName : System.String
Name : StartsWith
MemberType : Method
Definition : bool StartsWith(string value), bool StartsWith(string value, System.StringComparison comparisonType),
bool StartsWith(string value, bool ignoreCase, cultureinfo culture)
TypeName : System.String
Name : Substring
MemberType : Method
Definition : string Substring(int startIndex), string Substring(int startIndex, int length)
TypeName : System.String
Name : ToBoolean
MemberType : Method
Definition : bool IConvertible.ToBoolean(System.IFormatProvider provider)
TypeName : System.String
Name : ToByte
MemberType : Method
Definition : byte IConvertible.ToByte(System.IFormatProvider provider)
TypeName : System.String
Name : ToChar
MemberType : Method
Definition : char IConvertible.ToChar(System.IFormatProvider provider)
TypeName : System.String
Name : ToCharArray
MemberType : Method
Definition : char[] ToCharArray(), char[] ToCharArray(int startIndex, int length)
TypeName : System.String
Name : ToDateTime
MemberType : Method
Definition : datetime IConvertible.ToDateTime(System.IFormatProvider provider)
TypeName : System.String
Name : ToDecimal
MemberType : Method
Definition : decimal IConvertible.ToDecimal(System.IFormatProvider provider)
TypeName : System.String
Name : ToDouble
MemberType : Method
Definition : double IConvertible.ToDouble(System.IFormatProvider provider)
TypeName : System.String
Name : ToInt16
MemberType : Method
Definition : int16 IConvertible.ToInt16(System.IFormatProvider provider)
TypeName : System.String
Name : ToInt32
MemberType : Method
Definition : int IConvertible.ToInt32(System.IFormatProvider provider)
TypeName : System.String
Name : ToInt64
MemberType : Method
Definition : long IConvertible.ToInt64(System.IFormatProvider provider)
TypeName : System.String
Name : ToLower
MemberType : Method
Definition : string ToLower(), string ToLower(cultureinfo culture)
TypeName : System.String
Name : ToLowerInvariant
MemberType : Method
Definition : string ToLowerInvariant()
TypeName : System.String
Name : ToSByte
MemberType : Method
Definition : sbyte IConvertible.ToSByte(System.IFormatProvider provider)
TypeName : System.String
Name : ToSingle
MemberType : Method
Definition : float IConvertible.ToSingle(System.IFormatProvider provider)
TypeName : System.String
Name : ToString
MemberType : Method
Definition : string ToString(), string ToString(System.IFormatProvider provider), string
IConvertible.ToString(System.IFormatProvider provider)
TypeName : System.String
Name : ToType
MemberType : Method
Definition : System.Object IConvertible.ToType(type conversionType, System.IFormatProvider provider)
TypeName : System.String
Name : ToUInt16
MemberType : Method
Definition : uint16 IConvertible.ToUInt16(System.IFormatProvider provider)
TypeName : System.String
Name : ToUInt32
MemberType : Method
Definition : uint32 IConvertible.ToUInt32(System.IFormatProvider provider)
TypeName : System.String
Name : ToUInt64
MemberType : Method
Definition : uint64 IConvertible.ToUInt64(System.IFormatProvider provider)
TypeName : System.String
Name : ToUpper
MemberType : Method
Definition : string ToUpper(), string ToUpper(cultureinfo culture)
TypeName : System.String
Name : ToUpperInvariant
MemberType : Method
Definition : string ToUpperInvariant()
TypeName : System.String
Name : Trim
MemberType : Method
Definition : string Trim(Params char[] trimChars), string Trim()
TypeName : System.String
Name : TrimEnd
MemberType : Method
Definition : string TrimEnd(Params char[] trimChars)
TypeName : System.String
Name : TrimStart
MemberType : Method
Definition : string TrimStart(Params char[] trimChars)
TypeName : System.String
Name : Chars
MemberType : ParameterizedProperty
Definition : char Chars(int index) {get;}
TypeName : System.String
Name : Length
MemberType : Property
Definition : int Length {get;}
伊势:
TypeName : System.Management.Automation.PSCustomObject
Name : Equals
MemberType : Method
Definition : bool Equals(System.Object obj)
TypeName : System.Management.Automation.PSCustomObject
Name : GetHashCode
MemberType : Method
Definition : int GetHashCode()
TypeName : System.Management.Automation.PSCustomObject
Name : GetType
MemberType : Method
Definition : type GetType()
TypeName : System.Management.Automation.PSCustomObject
Name : ToString
MemberType : Method
Definition : string ToString()
TypeName : System.Management.Automation.PSCustomObject
Name : param1
MemberType : NoteProperty
Definition : string param1=stuff
TypeName : System.Management.Automation.PSCustomObject
Name : param2
MemberType : NoteProperty
Definition : string param2=stuff2
所以它就像外壳无法识别 PSCustomObject。我确实修改了脚本以使用 Hashtables,它工作正常。但是两个参数都有可能重复键,所以我需要一个自定义对象。我需要为 PSCustomObject 导入一个模块吗?
【问题讨论】:
-
如果在 PSCustomObject 中的每个属性后面加上分号会怎样?
-
没什么区别,不过谢谢!
-
@vrdse 名称值 ---- ----- PSVersion 5.1.14393.1944 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.14393.1944 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 序列化版本 1.1.0.1
-
如果它在shell中不起作用,它就不起作用。您不应将 ISE 用作测试环境。它作为编辑器工作得很好,但就是这样。
-
说真的,$listofobjs 是从哪里来的?
标签: powershell