This is an example of a WordPress page, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many pages like this one or sub-pages as you like and manage all of your content inside of WordPress.
Advertisements
Hi Richard,
i just stumbled abaout a tweet where you mentioned your PS Admin Modules
As Modules are my next thing i want to add to my PS learning curve i had a quick look what you published on Codeplex.
In your PAMSysInfo.psm1 i saw in your Get-Bus function some things which are quite new to me:
#================================ Your Function:
$bustype = DATA {
ConvertFrom-StringData -StringData @’
-1 = Undefined
0 = Internal
1 = ISA
2 = EISA
3 = MicroChannel
4 = TurboChannel
5 = PCI Bus
6 = VME Bus
7 = NuBus
8 = PCMCIA Bus
9 = C Bus
10 = MPI Bus
11 = MPSA Bus
12 = Internal Processor
13 = Internal Power Bus
14 = PNP ISA Bus
15 = PNP Bus
16 = Maximum Interface Type
‘@
}
function Get-Bus {
param(
[string]$computer=”.”
)
Get-WmiObject -class Win32_Bus -ComputerName $computer |
sort BusType, Busnum | select BusNum,
@{Name=”Bus type”; Expression={$bustype[“$($_.BusType)”]}},
DeviceID, PNPDeviceID
}
#===============================
Som Questions about your code:
1)
$bustype = DATA {
-> Keyword DATA: what is this about? How do you call this Technique (So i can google for?) Are there more Keywords beside DATA?
2)
Think that you used:
$bustype = DATA { … ConvertFrom-StringData
and not just a Hashtable like:
$bustype = @{
-1 = “Undefined”
0 = “Internal”
1 = “ISA”
2 = “EISA
}
is , because you just did not like to write the ” ” or is ther another special reason why you did that? Think Both way’s produce a Hashtable as far as i saw it.
3)
And My last question is, why you placed the $bustype assignment outside your function and not inside?
Hope you find some minutes to answer my questions 😉
Actually Question Nr 4. about Modules in General came into my mind while writing this Email 🙂
4)
You have Several Modules PAMSysInfo, PAMShares, …. I all would place in my modules Folder and then load with Import-Module..
I think i will also split up my scripts to multiple Modules.
What i like to do finally if possible is Following:
Having a Folder WindowsPowershell\Modules\PAM , and there are my “Sub”ModulesFolder WindowsPowershell\Modules\PAM\PAMSysInfo, WindowsPowershell\Modules\PAM\PAMShares, …
In the PAM folder itself i have a Preference File where i set the individual Submodules to load or not e.g.
ModulesToImport = @{
PAMSysInfo = $true
PAMShares = $true
PAMbla = $false
}
So with a “Import-Module PAM” , all specified SubModules would be loaded. (Like said before, if this is Possible at all 😉 )
Maybe you like the idea as well for your PAM Modules.. I Think PSCX uses such a technique but i had just a very quick look to PSCX Structure..
Thanks for you PS Community work! Really enjoy your blog, SG2011, Tweets, …
Regards from Munich, Germany
Thomas
luesec@googlemail.com
I have tried to use you code
$acmsk = DATA {
ConvertFrom-StringData -StringData @’
1 = Read/List
2 = Write/Create File
4 = Append/Create Subdirectory
8 = Read extended attributes.
16 = Write extended attributes.
32 = Execute file/Traverse directory
64 = Delete directory
128 = Read file attributes.
256 = Change file attributes.
65536 = Delete
131072 = Read access to the security descriptor and owner.
262144 = Write access to the discretionary access control list (DACL).
524288 = Assigns the write owner.
1048576 = Synchronizes access, allows a process to wait for an object to enter the signaled state.
‘@
}
$flags = @(1,2,4,8,16,32,64,128,256,65536,131072,262144,524288,1048576)
$share = Get-WmiObject -Class Win32_Share -computername ‘fwcsqlbk01vp’
$mast = Invoke-WmiMethod -InputObject $share -Name GetAccessMask
foreach ($flag in $flags){
if ($mask.ReturnValue -band $flag) {
Write-Host $acmsk[“$($flag)”]
}
}
but I get the following error message
Invoke-WmiMethod : Cannot convert ‘System.Object[]’ to the type ‘System.Management.ManagementObject’ required by parameter ‘InputObject’. Specified method is not supported
.
At H:\powershell\Untitled3.ps1:23 char:38
+ $mast = Invoke-WmiMethod -InputObject <<<< $share -Name GetAccessMask
+ CategoryInfo : InvalidArgument: (:) [Invoke-WmiMethod], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.InvokeWmiMethod
I can't figure out why this does not work.
Not sure where this code came from as I can’t find the original post.
The orginal code was this
$acmsk = DATA {
ConvertFrom-StringData -StringData @’
1 = Read/List
2 = Write/Create File
4 = Append/Create Subdirectory
8 = Read extended attributes.
16 = Write extended attributes.
32 = Execute file/Traverse directory
64 = Delete directory
128 = Read file attributes.
256 = Change file attributes.
65536 = Delete
131072 = Read access to the security descriptor and owner.
262144 = Write access to the discretionary access control list (DACL).
524288 = Assigns the write owner.
1048576 = Synchronizes access, allows a process to wait for an object to enter the signaled state.
‘@
}
$flags = @(1,2,4,8,16,32,64,128,256,65536,131072,262144,524288,1048576)
$share = Get-WmiObject -Class Win32_Share -Filter “Name=’Test'”
$mask = Invoke-WmiMethod -InputObject $share -Name GetAccessMask
foreach ($flag in $flags){
if ($mask.ReturnValue -band $flag) {
Write-Host $acmsk[“$($flag)”]
}
}
By leaving out the filter you get all shares returned which means that the Invoke-WmiMethod fails because you are passing it an array of share objects. If you want all shares then change the code to this
$computer = “$env:COMPUTERNAME”
$acmsk = DATA {
ConvertFrom-StringData -StringData @’
1 = Read/List
2 = Write/Create File
4 = Append/Create Subdirectory
8 = Read extended attributes.
16 = Write extended attributes.
32 = Execute file/Traverse directory
64 = Delete directory
128 = Read file attributes.
256 = Change file attributes.
65536 = Delete
131072 = Read access to the security descriptor and owner.
262144 = Write access to the discretionary access control list (DACL).
524288 = Assigns the write owner.
1048576 = Synchronizes access, allows a process to wait for an object to enter the signaled state.
‘@
}
$flags = @(1,2,4,8,16,32,64,128,256,65536,131072,262144,524288,1048576)
Get-WmiObject -Class Win32_Share -ComputerName $computer |
foreach {
“`n $($_.Name)”
$mask = Invoke-WmiMethod -InputObject $_ -Name GetAccessMask
foreach ($flag in $flags){
if ($mask.ReturnValue -band $flag) {
Write-Host $acmsk[“$($flag)”]
}
}
}
Hi Richard,
Since you have been doing a series of articles around AD Commands, I’d thought I’d pose a question to you. I found the following code on the Tech Net site for Determining a users last logon time: http://technet.microsoft.com/en-us/library/dd378867(WS.10).aspx
Import-Module ActiveDirectory
function Get-ADUserLastLogon([string]$userName)
{
$dcs = Get-ADDomainController -Filter {Name -like “*”}
$time = 0
foreach($dc in $dcs)
{
$hostname = $dc.HostName
$user = Get-ADUser $userName | Get-ADObject -Properties lastLogon
if($user.LastLogon -gt $time)
{
$time = $user.LastLogon
}
}
$dt = [DateTime]::FromFileTime($time)
Write-Host $username “last logged on at:” $dt }
Get-ADUserLastLogon -UserName SaraDavis
While the script will come back with the date and the time like this:
SaraDavis last logged on at: 1/15/2012 7:28:56 PM
I’d like to get the DC name in there as well. My feable attampts keep just returning the first DC name after the correct time.
This would be helpful in other scripts that I have for searching for inactive users or computers, to make sure that the account is truely aged out, and just not reporting that from the DC the script runs against.
Thanks in advance, and thanks for actively posting on your blog, it’s awesome!!
Jerame
Hi Jerame
The code you copied from TechNet is incorrect.
I’ve posted a function here
https://richardspowershellblog.wordpress.com/2012/01/18/last-logon-time/
that will do the job
Thank you very much!!!
You are very welcome. thanks for the feedback on the blog
Hi Richard,
We are currently in the process of readdressing our network to a 10.x.x.x network. I created a script to export my printer ports, and then would change them to the new IP’s, and I have modified script CreatePrinters.ps1 (http://poshcode.org/1462) to create my ports. This has worked well for many sites. I’m now starting to hit some of my larger sites, and want to script the change of the printers to use the new ports (which are already created) to speed up our migration time.
This works for one printer:
$Print = Get-WMIObject Win32_Printer -Filter “Name=’MyPrinterName'”
$Print.Port = “10.x.x.x”
$Print.Put()
So now I’m trying to pull the Printer Name and Port from a csv file to run through all of them. I’m hitting a snag passing the printer name into the WMI object:
function ChangePrinterPort {
$Server = $args[0]
$PrintName = $args[1]
$Print.Printer = Get-WMIObject Win32_Printer -Filter “Name=’$PrintName'”
$Print.Port = $args[2]
$Print.Put()
}
$Printers = Import-Csv c:\Scripts\Printers\NewPrintPorts.csv
foreach ($Printer in $Printers) {
ChangePrinterPort $Printer.Printserver $Printer.PrinterName $Printer.PortName
}
Would you have a suggestion as to how to correct this? I didn’t see this in Chapter 10 of PowerShell and WMI 🙂
Thanks,
Jerame
Hi Richard,
Just wanted to say thanks for sharing your knowledge. I like your books Powershell in Practice and Powershell and WMI.
Soyka
Hi Richard
I am trying to figure out how to use Power Shell to update the Cost Center field for a user?
Ideally, should be able to say update cost centre 252525 for all 10 users: user1, user2, user3 etc.
Any help would be appreciated 🙂
Thanks
I can’t see a Cost Center field in Active Directory? Is this an application data store you’re trying to update?
Hello Richard,
I´m very interested in your book “Powershell and wmi” but I can´t find it in Spanish. I guess no edition in spanish… Could you confirm me?
Thanks
Sorry – only available in English