首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell脚本。从select语句修改日期

问Powershell脚本。从select语句修改日期
EN

Stack Overflow用户
提问于 2014-03-22 05:11:42
回答 1查看 72关注 0票数 0

这是我们正在使用的当前脚本

代码语言:javascript
复制
import-module activedirectory

Get-Aduser -SearchBase "DC=XXXX,DC=LOCAL" -Properties * -Filter {employeeType -like "User"}  | 
    Select-Object givenname, sn, SAMAccountName, mail, idautoPersonEndDate, 
        idautoPersonBirthdate, employeeNumber, employeeType, 
        @{n='OU';e={($_.canonicalname -Split "/")[-4]}} | 
    export-csv "T:\IT\Techs\Userlists\destinystudents.csv" -NoTypeInformation

我们尝试得到的是输出如下所示的idautoPersonEndDate。

代码语言:javascript
复制
If idautoPersonEndDate <= today-60 then write "8-31-2013" else "do nothing"

我们要将.csv文件导入到的软件需要该字段的静态日期,并且帐户在删除之前会保持活动状态一段时间。

我开始做这个的唯一方法是

代码语言:javascript
复制
$today = Get-date
$today.ToShortDateString()

Get-Aduser -SearchBase "DC=XXXX,DC=LOCAL" -Properties * -Filter {employeeType -like "User"} |
Select-Object @{n={[idautoPersonEndDate]}
if ($n -le $today)
{
    "this"
}
ELSE
{
    "NULL"
}

我可以完全出去吃午饭,看看我是如何思考这件事的。所以任何帮助都是很棒的。

EN

回答 1

Stack Overflow用户

发布于 2014-03-22 14:52:37

看起来您最终想要csv文件中的信息,所以我下面的示例创建了一个CSV文件。我希望你能用它来得到你想要的东西。

代码语言:javascript
复制
# faking data from active directory to make the sample possible to run without AD
$fakeDataFromActiveDirectory = @(
    New-OBject PSObject -Property @{ SamAccountName = "User1"; idautoPersonEndDate = (Get-Date 2012-01-01)};
    New-OBject PSObject -Property @{ SamAccountName = "User2"; idautoPersonEndDate = (Get-Date 2014-01-15)};
    New-OBject PSObject -Property @{ SamAccountName = "User3"; idautoPersonEndDate = (Get-Date 2013-12-20)};
    New-OBject PSObject -Property @{ SamAccountName = "User4"; idautoPersonEndDate = (Get-Date 2012-06-17)};
)

# using a fake "now" date, to make the sample runnable even in a couple of months
$now = Get-Date 2014-02-01
$maxAccountAge = New-TimeSpan -Days 60
$dateToCompareWith = $now - $maxAccountAge

# The "MyDateField" name can be named whatever you want your column to be named
# Also, the hashtable sent to the select statement can be shortened by using "N" and 
# "E" instead of "Name" and "Expression" if you want.
$dataWithConvertedDates = $fakeDataFromActiveDirectory | 
    Select SamAccountName, @{ Name = "MyDateField"; Expression = { 
            if ($_.idautopersonenddate -lt $dateToCompareWith)
            { 
                "8-31-2013"
            } 
            else 
            { 
                "do nothing" 
            } 
        } }

$dataWithConvertedDates | Export-Csv "C:\myoutput.csv" -NoTypeInformation
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22569613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档