Get Username from computer name using powershell and sccm - powershell

I'm attempting to do the following :
Query SCCM using Powershell for a computer name, then, within the output of that, get the UserName.
Right now, I am using :
Get-CMDevice -name <computername>
This returns the entire record. Within that is an item called "UserName". That's what I want to extract out.
It's been a very long time since working with powershell, let alone the SCCM plugins.

Either use the member reference operator (.):
(Get-CMDevice -Name AComputerName).UserName
or use Select-Object -ExpandProperty:
Get-CMDevice -Name AComputerName |Select-Object -ExpandProperty UserName

Afternoon,
You should just be able to put the command in brackets and select the property directly as shown below:
(Get-CMDevice -name <computername>).UserName

Related

PowerShell Command to Set Logging on IIS 10

On Windows Server 2016/IIS 10, I can do the following in the IIS Manager GUI with the Log File Format set to W3C:
[Web Server Name] → Logging → Select Fields → W3C Logging Fields (Standard Fields) → Check or uncheck the boxes next to Standard Fields like "User Name (cs-username)"
I would really like to be able to check or uncheck fields like cs-username from a PowerShell script. To that end, I'm trying to discover the path to these standard fields, so I can then set them:
Import-Module IISAdministration
$prop = Get-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST" -Filter /system.applicationHost/sites/sitedefaults" -Recurse
When I run this PowerShell script, it asks me to "Supply values for the following parameters: Name[0].
I think it's a bit funny that it's asking me for a name when I'm trying to discover the next path element or name. If I type in something like cs-username, it comes back with nothing, suggesting I'm in the wrong directory (assuming recurse is actually looking around).
Any thoughts?
This is what ended up working for me for IIS 10:
Set-WebConfigurationProperty `
-filter "/*/*/site/logfile" `
-name "logExtFileFlags" `
-value "Date, Time, ClientIp, etc.."
of course replacing "etc.." with all the desired fields.

Check AD users' network share mapping with PowerShell

I tried using net use, net share, among others - none of which return the expected output. So instead, I'm modifying a script I found to see which network drives/shares are mapped to a user the script is pushed to. Then I go to my log file, look at the data, and determine if the account is set up properly. Here's the current script:
Get-WmiObject Win32_MappedLogicalDisk -computer $env:COMPUTERNAME | select name, providername
Out-File -filepath "\\*UNC filepath*\Mapped_Drives_$env:USERNAME$(get-date -Format _MM-dd-yy" # "HH.mm.ss" "tt).txt"
When I run it, the log file returns empty and I'm not sure why. I changed "Out-File -filepath" to "Start-Transcript" which isn't working the way I want it to either (with too much verbose output). It outputs fine in my PowerShell ISE with all the proper shares listed, but doesn't work when I navigate to the logged output. What am I missing?
You must pipe the output into the logfile
$logfile = "\\*UNC filepath*\Mapped_Drives_$env:USERNAME$(get-date -Format _MM-dd-yy" # "HH.mm.ss" "tt).txt"
Get-WmiObject Win32_MappedLogicalDisk | select name, providername | Out-File $logfile
On a more general note I'd use the commands to fix the mapped drives right there and then, instead of just writing them to a logfile for later inspection.

Changing Windows local user regional settings via Powershell

I have created over 100 users using the wrong regional settings, therefore wrong date format. I wish to correct this without logging in as every user and going through the necessary steps.
I am looking for a way to do this in one go either via UI or via Powershell script.
Ideally I want a script that will ForEach the local users and run a command to change their language/regional settings.
NOTE: I am not using Active Directory therefore cannot use group policies.
Set your desired settings in one of the users, then Get those settings from
"HKEY_CURRENT_USER\Control Panel\International"
find the SID of each user then
Apply it to each user on:
"HKEY_USERS\[UserSID]\Control Panel\International"
To get The SID of the user you can use this Function:
Function GetSIDfromSAM()
{
Param(
[Parameter(mandatory=$true)]$userName
)
$myacct = Get-WmiObject Win32_UserAccount -filter "Name='$userName'"
return $myacct.sid
}
GetSIDfromSAM localuser
S-1-5-21-1837353773-20556466214-3321741005-1005
Then use the Foreach section (just for example)
New-PSDrive HKU Registry HKEY_USERS
Foreach ($SID in $SIDs)
{
Set-ItemProperty -Path "HKU:\$SID\Control Panel\International" -Name "LocaleName" -Value "en-us"
}
Of course you should update all the properties, you can use a switch section with all properties and values, or other methods, let me know if you need more help on this

Selecting specific IIS website using powershell

I currently have several websites running under IIS. For ease lets call them site1, site2, and site3
My powershell script accepts a parameter from the user of which number site to work with. I am attempting to turn off site3 with simply the parameter 3
I thought it would be something along the lines of
stop-website | where-object {$_.name -like "*3*"}
this line is executed, but brought to a prompt for "name"
The Name property seems to accept wildcards (although help states otherwise) so you should be able to stop it with this:
Stop-Website -Name *3*

Powershell Newbie: How do I filter results so that only the information I get back can be used in a pipeline?

Lets say I run a powershell command (in my case Group Policy related) but lets say I run this command:
PS C:>Get-GPO -All
and my output looks like:
DisplayName : My Named GPO
DomainName : mydomain.com
Owner : Domain Admins
Id : Random_GUID
...
How can I "filter" that command so that it only returns the lines relating to DisplayName? Is that possible or will I need to do some string parsing that's not available inside a pipeline operation? Because ultimately, I'm looking to use that DisplayName param to pipe to another command.
Thanks in advance!
You can use Select-Object (or select in short)
Get-GPO -All | Select DisplayName