从文件中获取用户列表以进行Powershell循环 [英] Get list of users from file for powershell loop

查看:22
本文介绍了从文件中获取用户列表以进行Powershell循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修改此PowerShell脚本,以允许从文本或CSV文件输入用户.我已直接从

我想我应该修改脚本的一部分,设置 $ user 的变量,以及 foreach($ users中的$ user)循环如何处理每个部分用户,但无法弄清楚.

至此...

允许从文本或CSV文件中输入用户.

……就是这样……

  $ users = Get-ADUser -Filter * 

...您只需将其替换为文件

  $ users = Import-Csv -Path $ UncToUserList.csv 

但是,由于您的问题,由于这是一个非常普通的基本PowerShell事情,我假设您对此非常陌生,并建议您使用 YouTube 以及免费提供的电子书,以尽可能避免学习周期中的混乱和沮丧./p>

此外,您永远不要运行任何不受信任的代码,或者您不完全理解的代码.仅在可以考虑生产之前完全恢复的测试环境中使用,以避免可能导致您发生RPE错误的事情-恢复生产事件".

I'm trying to modify this PowerShell script to allow input of users from a text or CSV file. I've pulled the script directly from Tim Rhymer.

This is the original script:

Import-Module ActiveDirectory

function Get-ADUsersLastLogon() {
  $dcs = Get-ADDomainController -Filter {Name -like "*"}
  $users = Get-ADUser -Filter *
  $time = 0
  $exportFilePath = "c:lastLogon.csv"
  $columns = "name,username,datetime"

  Out-File -FilePath $exportFilePath -Force -InputObject $columns

  foreach ($user in $users) {
    foreach ($dc in $dcs) { 
      $hostname = $dc.HostName
      $currentUser = Get-ADUser $user.SamAccountName | Get-ADObject -Server $hostname -Properties lastLogon

      if ($currentUser.LastLogon -gt $time) {
        $time = $currentUser.LastLogon
      }
    }

    $dt = [DateTime]::FromFileTime($time)
    $row = $user.Name + "," + $user.SamAccountName + "," + $dt

    Out-File -FilePath $exportFilePath -Append -NoClobber -InputObject $row

    $time = 0
  }
}

Get-ADUsersLastLogon

I'm thinking I should modify the portion of the script the sets the variable of $user and how the foreach ($user in $users) loop processes each user, but can't figure it out.

解决方案

As for this...

allow input of users from a text or CSV file.

… that is all that this is...

$users = Get-ADUser -Filter *

… You just replace that with your file

$users = Import-Csv -Path $UncToUserList.csv

Yet, by your question, since this is a really common, basic PowerShell thing, I am assuming you are very new to this and would advise really ramping up on PowerShell using all the freely available learning resources on TechNet MVA, MSChannel9 and YouTube, as well as freely available eBooks, as to avoid much confusion and frustration in your learning cycles as possible.

Also, you should never run any untrusted code, or code you do not fully understand. Use only in a test environment that you can fully recover from before ever considering production runs to avoid things that can cause you to have an RPE error --- 'Resume Producing Event'.

这篇关于从文件中获取用户列表以进行Powershell循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆