How to find a file location by file name, and file name only, using get_ciminstance in powershell? - powershell

I'm trying to write a script that will retrieve a specific file's properties across multiple computers. I was using get-childitem to do this until I realized that only retrieves locally. I've read that get-ciminstance can be used to do this for remote machines, however, all the examples I've seen use full paths to find the files. My script assumes the location could be anywhere on the C drive, so it only looks for the location based on the file's name. So far I've tried several variations of code using get-ciminstance, but all either produce nothing or have the wrong query structure.
Here's what I have right now, and it's what I believe is the closest to being correct, but I'm not sure:
Get-CimInstance -ComputerName $PC -ClassName CIM_DataFile | select Name | Where-Object { $_.Name -like "install.properties"}
If anyone can point me in the right direction, it would be greatly appreciated. Thank you.

Here's a nice link. https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/accessing-individual-files-and-folders-remotely-via-wmi Name is the full path, with double backslashes. The filter language is like sql. You can't use invoke-command?
Get-CimInstance CIM_DataFile -Filter 'name = "c:\\users\\admin\\foo\\file.json"'

Related

Powershell Command for AD export

I have been searching for info, I am not even sure if what I am doing is feasible. All I want to do is export AD all computers. I have the command "Get-ADComputer -Filter "*" | Export-CSV -Path c:\Users\USERNAME\documents\Computers.csv" and this works fine, BUT...
I want to be able to add a pull of serial numbers 1) either through the attribute editor in AD or 2) by using Get-WmiObject Win32_BIOS | Select SerialNumber and having it the serial number displayed in the .csv file.
I have tried so many combinations of commands, but nothing works. I even tried to add in the "ForEach" option.
I have used Get-WmiObject Win32_ComputerSystem | Select Manufacturer,Model to get get the info from my own computer, but if I am able to get it exported into the .csv as well - not required, but would be a bonus for my reports.
If I can get a command that combines basic AD computer properties & at least serial number, I would greatly appreciate it. And if the command can also include what to add in the get the make/model of the computer, I would appreciate it more!
Thank you for your time and assistance!

How to change ManagedBy owner from one user to another one for 150+ groups using power shell

I would like to change the Active Directory Group tab ManagedBy user to another one. With PowerShell script, I exported the groups with the old owner (>150) to a csv file. Now I need to change the owner of those groups using the csv file as input.
I don`t have much experience with scripting, I appreciate any help.
Thanks!
The task is very easy with PowerShell. You didn't show an example of the CSV data you exported so an example may not be exact. However, I assume you exported the default output of Get-ADGroup it might look something like this
(Import-Csv C:\temp\managedBy.csv).DistinguishedName| Set-ADGroup -ManagedBy <NewManager's DN>
Note: I like to use the DistinguishedName for these things but samAccountName should also work.
(Import-Csv C:\temp\managedBy.csv).samAccountName | Set-ADGroup -ManagedBy <NewsamAccountName>
Note: Again with the assumption that your Csv data is a direct export Get-ADGroups's output. You cannot pipe Import-Csv directly to Get/Set-ADGroup as the latter will have trouble determining which property to bind to the -Identity parameter.
However, I would point out you really don't need the intermediate Csv file. You can query AD directly for groups managed by the old manager and pipe that to a command to change the owner.
Get-ADGroup -Filter "ManagedBy -eq '<OldOwner'sDN>'" |
Set-ADGroup -ManagedBy "<NewOwner'sDN"
Note: Again you may be able to get away with using the samAccountName instead of the DN.
Note: You can add the WhatIf parameter to the Set-ADGroup` command to preview what will happen before actually running it.

Get current version number of specified program

I started with this, on a recommendation from a friend
Get-WmiObject win32_product | ft name, version
But then I found this, which gives me pause.
A little research led me to this
wmic product where "Name='Revit 2018'" get name,version
Which works as far as the data gathered. And yes, I am looking for a different program in this example. in any case, once I had good info using WMIC I tried to get the data into a variable so I could get just the version number, but the data formatting is something I have never seen before. I was hoping for a simple solution, like
$object = wmic product where "Name='Revit 2018'" get name,version
$object.version
But only the result is an array with 6 items, and only one seems to be the actual data line, and that's a single line, not two properties. And, I really wonder if an old command line utility is the right answer here. If it really is the best way to do this, is there a trick to converting the raw data to something more, PowerShelly? And if it's not the best way to get this info, what is? Is that scary link real, or is Get-WmiObject win32_product actually safe? And if so, is there a way to filter on a specific name, to speed things up? And indeed, Get-WmiObject doesn't work as I was expecting, as
$object = Get-WmiObject win32_product | ft name, version
foreach ($item in $object) {
Write-Host "$($item.version)"
}
Doesn't work as expected at all.
EDIT: This seems to be working as expected, which is progress.
$version = (Get-WmiObject win32_product -filter:"Name = 'Revit 2018'" | Select-Object -property:*).version
Write-Host "$version!"
I guess the question is really, is this a safe and consistent approach, or is there a better one?
Why not use the registry?
Set-Location HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall
$app = Get-ChildItem | Where-Object { $_.GetValue("DisplayName") -match 'YourSoftware' }
$app.GetValue("DisplayVersion")
Or
Set-Location HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall
$apps = Get-ChildItem
foreach ($app in $apps) {
$app.GetValue("DisplayName","DisplayVersion")
}
Note: You'll also need to check the SysWow64 registry location as well
HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\
Note: Not all items will have a display version in which case you always have the option of looking in the installation directory for the executable itself, which should have a version on it.

Need to scan all domain computers for .pst files

I am new to powershell sctipting, like Brand new. I have some experience using Exchange powershell but thats always been for very specific items like adjust calendar permissions and such. Nothing to robust.
Currently I am working on a powershell script to push out via Group policy that will run a a search on each domain PC. I've been getting help from a co-worker but he isn't available right now and I have a hard time following him sometimes. I am this site and its user might be able to assist me. What I am trying to do(and I believe I am close to) is pulling a list of drives for each computer on the domain. Once I pull that list O pipe it into a variable and then do a search on that variable for any files that end with .pst. Once the search is complete if there were results from the search a file should be created with the FUllname"path" of each file and the computer name should be used for naming the file. If there are no results form the search the file would be empty but the filename should still be named after t he computer. I believe I have gotten everything correct except that I do not know how to name the file based on the computer name. Thank you for your time and help with this.
Here is my code so far:
$drives=Get-WmiObject -query "SELECT * from win32_logicaldisk where
DriveType = '3'" | select deviceid
foreach ($drive in $drives){
$pstfound=Get-ChildItem $drive.deviceid *.pst -recurse | select
fullname
$pst+=$pstfound
}
IF ($pst -eq $null) {
$pst | Out-File \\"Servername"\Searchresults\Null
} Else {
$pst | Out-File \\"Servername"\Searchresults\HasItems
}
Thank you. I wasn't initially planning on using the UNC path but changed it up anyways and I think that will make it easier to go through later. I also figured out my issue for naming the file generated after the computer it ran on. I just set a variable $hostname=hostname and then set the files as \$hostname.csv

Win32_Volume Name doesn't match a mount point folder

We have a server with 20+ mount points. All the mount points are physically working fine, however certain ones are showing a 'Name' of \?\Volume{GUID} in Win32_Volume. In addition, in Disk Management, these same ones aren't showing a path when you right click on them and select Change Drive Letter and Paths. But again, the MP is up and working.
Has anyone ever seen this before? It's only an issue because I use a script to return all the MPs underneath a particular drive letter that uses Win32_Volume and it fails for this drive because they are listed as \?\Volume{GUID} instead of their path like all the others.
For anyone interested, here's the Powershell script:
Get-WMIObject -Class "Win32_Volume" -Filter "DriveType=3 AND (Name LIKE 'X:\\%')" | Select Name, Capacity, Freespace | ConvertTo-XML -As string'
They show up that way if they don't have a drive letter assigned. You could try searching for the label instead if it's the same for x:\ on all servers.