powershell如何找出一个变量是否是另一个变量的一部分

追求卓越

我已经在下面粘贴了我的代码并取出了所有已经在工作的东西,所以我只有没有按预期工作的部分。

我试图将EmployeeID, 来自 csv,Description放在 AD中的字段前面我可以让那部分工作,但是If我尝试检查$ID描述中是否已经存在的语句的开头失败了;它只是在每次脚本运行时不断添加它。

我曾尝试将$ID$Description类型都作为字符串使用Out-String,但我已将其排除在外,但结果相同。我已经尝试过-notcontains, -notmatch, 和-notlike(我认为这是正确的使用方法),但没有任何效果。我什至将我的变量放在一个文本文件中,以确保它们提取正确的信息。

我仍在学习 Powershell 的所有复杂功能。谁能看到我做错了什么?

# Get script Start Time (used to measure run time)
$startDTM = (Get-Date)

#Null out variables
$Users = $Null
$ID = $Null

$Users = Import-Csv .\ImportADUsers\Test-Import-user-data.csv

Import-Module ActiveDirectory

$path = Split-Path -parent ".\ImportADUsers\*.*"

#Create log date
$logdate = Get-Date -Format yyyy-MM-dd-THH.mm.ss

$logfile = $path + "\logs\$logdate.logfile.txt"

# Enumerate the users, one line at a time.
# This assumes the first line is a header line defining the fields.
ForEach ($User In $Users)
{
    # Retrieve values from the csv.
    $ID = $User.HRRef

         # Retrieve the sAMAccountName of the user from AD.
    $UserDN = (Get-ADUser -LDAPFilter "(employeeID=$ID)").sAMAccountName
    $ID | Out-File $logfile -Append
    $IDString = $ID | Out-String

    #Retrieve the Description of the user from AD.
    $Description = Get-ADUser -Identity $UserDN -Properties description
    $Description = $Description.description | Out-String
    $Description | Out-File $logfile -Append

# Make sure there is only one user with this employeeID.
    If ($UserDN.Count -eq 1)
    {

       IF ($Description -notlike $IDString) {Set-ADUser -Identity $UserDN 
           -Description "$($ID) - $($Description)" } 

    }
    Else {"User with ID $ID either not found, or more than one user found." 
    | Out-File $logfile -Append}
    #Log error for users that are not in Active Directory or EmployeeID 
    #found more than once
}
#Finish
#The lines below calculates how long it takes to run this script
# Get End Time
$endDTM = (Get-Date)

# Echo Time elapsed
"Elapsed Time: $(($endDTM-$startDTM).totalminutes) minutes"

#Append the minutes value to the text file

"Import took $(($endDTM-$startDTM).totalminutes) minutes to complete." | 
 Out-File $logfile -Append
 #SCRIPT ENDS
马克西米利安·伯兹利

您的字符串比较不正确。下面是如何修复它。

改变: -notlike $IDString => -notlike "*$ID*"

ForEach ($User In $Users)
{
    # Retrieve values from the csv.
    $ID = $User.HRRef
    $ID | Out-File $logfile -Append

    # Retrieve the SAMAccountName of the user from AD.
    $UserDN = (Get-ADUser -LDAPFilter "(employeeID=$ID)").SAMAccountName

    #Retrieve the Description of the user from AD.
    $Description = (Get-ADUser -Identity $UserDN -Properties description).Description
    $Description | Out-File $logfile -Append

    # Make sure there is only one user with this employeeID.
    If ($UserDN.Count -eq 1 -and $Description -notlike "*$IDString*")
    {
        Set-ADUser -Identity $UserDN -Description "$ID - $Description"
    }
    Else
    {
        "User with ID $ID either not found, or more than one user found." | Out-File $logfile -Append
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

批量将一个变量与另一个变量的一部分匹配

如何剪切变量的一部分并将其放入另一个变量

Javascript - 更改作为另一个变量定义一部分的变量值

用另一个变量的值替换变量名的一部分?

使用变量作为另一个变量名的一部分

批处理文件变量的另一个变量的一部分

Django使用模板变量的值作为另一个变量名的一部分

PHP检查是否另一个数组的一部分

检查一个词是否是另一个词的一部分

AWK-如何将变量用作另一个变量名称的一部分

如何从另一个线程锁定方法的一部分?

如何从另一个路径中删除路径的一部分?

如何使Automapper映射另一个表的一部分的属性

如何通过一个环境变量作为命令的一部分

如何提取char的一部分然后将其与C中的另一个变量进行比较

Excel向导:如何创建一个公式,以将数字的一部分乘以一个单元格,另一部分乘以另一个

将QTableWidget的一部分复制到另一个

根据另一个索引列表删除列表的一部分

在另一个图像后面显示图像的一部分

将 div 的一部分放在另一个下面

将设置的一部分放在另一个文件中

用另一个位图擦除位图的一部分

仅从另一个脚本中获取脚本的一部分?

从另一个IList的一部分快速创建List

从另一个组件重新渲染组件的一部分

查找文本的一部分并写入另一个文件

仅等待另一个线程的一部分-Java

卡在“如果”测试中:另一个代码的一部分

pygame - 来自另一个表面的一部分的表面