Powershell example 5

SetPermswithCACLS.ps1

#SetPermsWithCACLS.ps1
# CACLS rights are usually
# F = FullControl
# C = Change
# R = Readonly
# W = Write

$StartingDir=Read-Host " What directory do you want to start at?"
$Right=Read-Host " What CALCS right do you want to grant? Valid choices
are F, C, R or W"
Switch ($Right) {
  "F" {$Null}
  "C" {$Null}
  "R" {$Null}
  "W" {$Null}
  default {
    Write-Host -foregroundcolor "Red" `
    `n $Right.ToUpper() "is an invalid choice. Please Try again."`n
    exit
  }
}

$Principal=Read-Host " What security principal do you want to grant" `
"CACLS right"$Right.ToUpper()"to?" `n `
"Use format domain/username or domain/group"

$Verify=Read-Host `n "You are about to change permissions on all" `
"files starting at"$StartingDir.ToUpper() `n "for security"`
"principal"$Principal.ToUpper() `
"with new right of"$Right.ToUpper()"."`n `
"Do you want to continue ? [Y,N]"

if ($Verify -eq "Y") {

 foreach ($file in $(Get-ChildItem $StartingDir -recurse)) {
  #display filename and old permissions
  write-Host -foregroundcolor Yellow $file.FullName
  #uncomment if you want to see old permissions
  #CACLS $file.FullName

  #ADD new permission with CACLS
  CACLS $file.FullName /E /P "${Principal}:${Right}" >$NULL

  #display new permissions
  Write-Host -foregroundcolor Green "New Permissions"
  CACLS $file.FullName
 }
}

GetLDAPUsers.ps1

#GetLDAPUsers.ps1

$user=read-host "What user credentials do you want to use for" `
"authentication to the" `n `
"domain controller? Use format domain/username."
$cred=get-credential $user
$server=read-host "What domain controller do you want to connect to?"

$rc=read-host "Do you also want to save output to a text file? [YN]"
if ($rc -eq "Y") {
$file=read-host "Enter the filename and path"
write-host "Connecting to" $server "as" $user
get-wmiobject -class ds_user -namespace root/directory/ldap `
-computername $server -credential $cred | `
select-object DS_Name,DS_distinguishedname,DS_sAMAccountname |`
tee-object -file $file
}
else
{
write-host "Connecting to" $server "as" $user
get-wmiobject -class ds_user -namespace root/directory/ldap `
-computername $server -credential $cred | `
select-object DS_Name,DS_distinguishedname,DS_sAMAccountname
}

 

 

CreateUser.ps1

#CreateUser.ps1
#specify the OU where you want to create the account
$OU=[ADSI] "LDAP://OU=Testing,DC=MyCo,DC=Local"

#using the ADSI type specifier
#Add the user object as a child to the OU
$newUser=$OU.Create("user","CN=Francis Bacon")
$newUser.Put("sAMAccountName","fbacon")
#commit changes to Active Directory
$newUser.SetInfo()
#set a password
$newUser.SetPassword("P@ssw0rd")
$newUser.SetInfo()
#Define some other user properties
$newUser.Put("DisplayName","Francis Bacon")
$newUser.Put("UserPrincipalName","Fbacon@MyCo.com")
$newUser.Put("GivenName","Francis")
$newUser.Put("sn","Bacon")
#enable account = 544
#disable account = 546
$newUser.Put("UserAccountControl","544")
$newUser.Put("Description","Created by PowerShell "`
+(get-date).ToString())

#commit changes to Active Directory
$newUser.SetInfo()
#flag the account to force password change at next logon
$newUser.Put("pwdLastSet",0)
$newUser.SetInfo()
 

AddToGroup.ps1

#AddToGroup.ps1

$Grp=[ADSI]"LDAP://CN=SAPIEN Authors,OU=SAPIEN,DC=MyCo,DC=local"
$NewUserDN="CN=Bill Shakespeare,OU=Testing,DC=MyCo,DC=local"

#create an array object from current group members
$grpMembers=@($Grp.Member)

#display current group membership
Write-Host "There are currently" $grpMembers.Count "members in" $Grp.Name
foreach ($user in $grpMembers) {$user}

Write-Host `n; Write-Host "Adding" $NewUserDN
($grp.Member).add($NewUserDN) > $NULL

#commit changes to Active Directory
$Grp.SetInfo()

#refresh object and display new membership list
$Grp.psbase.refreshCache()
$grpMembers=@($grp.Member)

#display new membership
Write-Host "There are now" $grpMembers.Count "members in" $grp.Name
foreach ($user in $grpMembers) {
 if ($user -eq $NewUserDN) {
  write-Host -foregroundcolor Green $user
 }
 else
 {
 write-Host $user
 }
}

 

ListWinNT.ps1

#ListWinNT.ps1
$member=[ADSI]"WinNT://MyServer"
 foreach ($item in $member.psbase.children) {
  if ($item.psbase.schemaclassname -eq "user") {
   Write-Host $item.Name
  }
}

 

SearchForAllUsers.ps1

#SearchForAllUsers.ps1
$searcher=New-object DirectoryServices.DirectorySearcher
$searcher.Filter="(&(objectcategory=person)(objectclass=user))"
$users=$searcher.FindAll()
#display the number of users
Write-Host "There are "$users.count"users in this domain."
#display each user's distinguishedname
foreach ($user in $users) {
 Write-Host $user.properties.distinguishedname
}

 

SearchForAllUsersAdvanced.ps1

#SearchForAllUsersAdvanced.ps1
$searcher=New-object DirectoryServices.DirectorySearcher
$searcher.Filter="(&(objectcategory=person)(objectclass=user))"
$users=$searcher.FindAll()
#display the number of users
Write-Host "There are "$users.count"users in this domain."
foreach ($user in $users) {
 foreach ($user in $users) {
  $entry= $user.GetDirectoryEntry()
  $entry |Select displayname,samaccountname,description,distinguishedname
 }
}

 

 

FindUserDN.ps1

#FindUserDN.ps1
$sam=Read-Host "What user account do you want to find?"
$searcher=New-Object DirectoryServices.DirectorySearcher
$searcher.Filter="(&(objectcategory=person)(objectclass=user)"`
+"(sAMAccountname="+$sam+"))"
$results=$searcher.FindOne()
if ($results.path.length -gt 1)
 {write-host $results.path}
else
 {write-host "User" $sam "was not found."}
 

 

时间: 2024-11-10 13:44:31

Powershell example 5的相关文章

使用AppVeyor CI 和PowerShell部署应用概述

开头语 关于如何为单一的ASP.NET web应用程序设置持续集成,你可以找到很多文章.这些文章都写到如何通过Web Deploy来构建完美的环境来部署简单.只需稍作修改VS.NET模板的web应用程序.任何东西在这一完美环境下都能顺利进行. 但是,真正部署应用程序的话却并非易事.总是有问题不断出现在以下情况中:当需要在注册表(Registry)或自定义文件夹中配置设置,或者你需要部署到Web集群时. 本文中,我们通过使用PowerShell远程处理(PowerShell remoting)和A

超越PowerShell PowerGUI使用心得

我们知道PowerShell将成为cmd的继任者,微软已经将其集成到Windows Server 2008和Windows 7中.不知道大家有没有注意到,在Windows 7中除了PowerShell之外,还有一个名为PowerGUI的工具,从名称上看其实它就是基于图形用户界面的PowerShell,是一款第三方PowerShell增强软件.当然,也不仅仅如此.因为PowerGUI提供了高效率的NET风格的脚本开发环境,而且也便于脚本的调试,这对于一个PowerShell脚本开发者来说,使用Po

如何利用PowerShell分析SharePoint WebApplication体系结构

之前一篇文章<两张图看清SharePoint 2013 Farm 逻辑体系结构>谈到Web Application,Content Database,Site Collection的关系.有了这个逻辑结构图之后,这篇文章将使用PowerShell,来更加直观的展现SharePoint WebApplication的体系结构. SharePoint WebApplication Structure 从上图可以看出,一个WebApplication可以包含多个Content Database,可以

PowerShell如何批量签入SharePoint Document Library中的文件

由于某个文档库设置了编辑前签出功能,导致批量导入文件时这些文件默认的状态都被签出了.如果手动签入则费时费力,故利用PowerShell来实现批量签入Document Library中的文件. Resolution Add-PSSnapin Microsoft.SharePoint.PowerShell function CheckInDocument([string]$url){ $spWeb=Get-SPWeb $url $spDocument=$spWeb.Lists.TryGetList(

如何通过PowerShell获取Site Collection下被签出的文件

由于权限的设置,当文件被签出时导致别人不可见了,这对校验文件个数的人来说着实是件烦恼的事.幸好利用PowerShell,可以获取Site Collection下被签出的文件. Resolution Add-PSSnapin Microsoft.SharePoint.PowerShell function GetAllCheckOutFiles([string]$siteUrl){ $spSite=Get-SPSite $siteUrl $spSite.AllWebs|%{ $_.Lists|wh

如何通过Windows 8 Powershell创建USB引导盘

大家可能已经比较熟悉了制作Windows USB启动盘进行操作系统引导和安装的方法,该方法一般需要第三方的工具例如UltraISO,Windows 7 USB Tool或者RM等加载安装ISO映像文件,拷贝安装Binary然后再用Diskpart磁盘管理工具封装启动分区等. 现在Windows8中,USB的启动盘制作更加简单,可以不再需要第三方工具或者Diskpart磁盘分区工具了,Windows 8本身就可以加载ISO或VHD(虚拟磁盘). 过去,采用的方式流程为:Diskpart分区,Lis

为 windows 服务器安装 PowerShell 管理库

最近在摸索一种命令方式备份Hyper-V里的虚拟机,因为平时备份都是手动关闭VM,然后Export,这样浪费很多的时间,于是折腾了下,想用命令实现自动化完成VM的备份. 查阅了下资料,看到了一个开源的软件能够用此Powershell Management Library里的命令完成大部分的虚拟机管理操作,在这里分享此网站: http://pshyperv.codeplex.com/ 但是在Import 此Module时出现如下错误: Import-Module : There were erro

2008R2core模式下安装PowerShell[为企业部署Windows Server 2008系列十一]

windows server 2008 R2版本发布一段时间了,再三斟酌后还是决定将2008R2的应用也写进本次2008 主题中来. 很多朋友喜欢core模式,甚至喜欢命令行,那么安装了core模式的你,再给core模式 的2008R2安装上PowerShell 那真是将命令行发挥到极致了(帅呆了!!). 对,我也是这样想 的! 那么下面我们一起来看看如何在core模式下安装PowerShell: 第一步:用下面的命 令列出当前服务器的功能和状态: DISM /Online /Get-Featu

精通Windows Server 2008多元密码策略之PowerShell篇

前言 在上两篇文章<精通Windows Server 2008 多元密码策略之ADSIEDIT篇>和<精通Windows Server 2008 多元密码策略之LDIFDE篇>中我向大家介绍了如何通过ADSIEDIT工具.活动目录用户和计算机管理单元和LDIFDE命令行工具创建.管理密码设置对象PSO.在这篇文章中,我将向大家展示如何使用Quese公司出品的针对AD管理的PowerShell来实现.管理多元密码策略. 按照惯例,为了让大家在操作的时候有一个清晰的思路,我将主要的操作

Windows PowerShell的12项功能

解答PowerShell:PowerShell原来的开发代号是Monad,原计划是用以替代Windows中的命令行工具,但是后来微软说它只是技术上的扩充.使用PowerShell,管理员可以做任何在图形界面下所做的事情.Windows PowerShell 1.0可以运行在Windows XP SP2.Windows Server 2003和Windows Vista上. 1. 内置Cmdlets (即"commandlets") Windows PowerShell中的所有Cmdle