Showing properties of share settings with Powershell - powershell

I need to check if this checkbox is enabled or disabled:
Is there anyway i can check this with powershell, because there something like 1400 shares I need to check?
Any help is welcome!

The cmdlet Get-SmbShare returns objects with losts of useful properties. One of which is called CachingMode.
To report on shares where the CachingMode is set to 'Manual' for instance, you can do
# as example only output on screen
Get-SmbShare | Where-Object {$_.CachingMode -eq 'Manual'} | Format-Table -Property Name, CachingMode
If you want to save this data to csv , remove the Format-Table cmd and do
Get-SmbShare | Where-Object {$_.CachingMode -eq 'Manual'} |
Select-Object Name,CachingMode |
Export-Csv -Path 'X"\Somewhere\ShareCaching.csv' -NoTypeInformation

Related

I have a .csv with thousands of emails, and I need to use Active Directory to find the state of a particular, custom variable, using powershell

I'm new and don't know enough about powershell.
I've got a .csv that is nothing but "EMAILS" for the header and some 6000 emails under it. (email1#company, email2#company, etc.)
I need to find the state of a particular, custom property for each one.
Individually, I know that I can run
Get-ADUser -Filter {mail -eq 'email#company'} -properties customproperty
in order to find one particular user's state.
I have been hitting my head against a wall trying to make it work with import-csv and export-csv, but I keep getting red all over my console.
Can someone point me an to example where import-csv and export-csv are used properly, with a command run against the contents?
Here's what I would do.
First, fetch all users that have email addresses in AD, and save them into a hashtable. This will make lookups absurdly faster and place less overall load on your domain controller. If you've got 100,000 user accounts it may not be the best option, but I expect that it will be in general.
$ADUsers = #{};
Get-ADUser -Filter "mail -like '*'" -Properties mail, customproperty | ForEach-Object {
$ADUsers.Add($_.mail, $_.customproperty);
}
Now you import the CSV and do lookup using a calculated property with Select-Object, and export it back out.
Import-Csv -Path $InputFile | Select-Object -Property emails, #{n='customproperty';e={$ADUsers[$_.emails]}} | Export-Csv -Path $OutputFile -NoTypeInformation;
So without seeing the code of what you posted, where I think you will run problems is with how interaction of two things.
The first will be that when you use the Import-CSV cmdlet. You will receive an array of objects with a property for each column and not an array of strings.
This is ties into the second part of the issue which is the sensitivity of the AD module filter. But the short answer is don't use {} inside the filter because it will break if you use {mail -eq $ImportedCSV.EMAILS}.
mklement0 has a wonderful answer that goes into the details. But a simple rule of thumb is "double quote outer, single quote" inner.
So you could expand the EMAILS property either with Select-Object -ExpandProperty EMAILS to have an array which works inside {} or you could use "mail -eq '$ImportedCSV.EMAILS'".
Here is an example with both the expansion and using the "property -eq '$variable'" style filter:
Import-CSV C:\Example\Path.csv |
Select-Object -ExpandProperty EMAILS |
ForEach-Object {
Get-ADUser -Filter "mail -eq '$_'" -Properties customproperty
} |
Select-Object mail,customproperty |
Export-CSV C:\Example\OutputPath.csv -NoTypeInformation
Please use below code
$csvInput = Import-CSV C:\Example\test.csv
Foreach ($line in $csvinput) {
Get-ADUser -Filter {mail -eq $line.mail} -Properties mail, customproperty |
Select-Object mail,customproperty |
Export-CSV C:\Example\OutputPath.csv -NoTypeInformation
}

Powershell ActiveDirectory Logging and import for easy rollback

Im writing a custom script from start to finish to search for users ,so i can disable and move them. (script is done and working)
What im trying to do now is to create a easy readable log which also has to be used as an easy rollback.
I.ex.:
Get-Aduser -Identity test -properties SamAccountName,MemberOf,GivenName,Surname
This will return something in line of:
DistinguishedName : CN=test,OU=OUy,OU=Lab Accounts,DC=domain,DC=org
Enabled : True
GivenName : test
Name : test
SamAccountName : test
Surname : test
Memberof : {CN=OU,OU=Norway,OU=Lab Accounts,DC=Domain,DC=org, CN=Domain Admins,CN=Users,DC=Domain,DC=org}
If i use:
$test2 = Get-ADUser -Identity test |
Select-Object -Property SamAccountName,GivenName,Surname,Memberof
which gives me:
SamAccountName GivenName Surname Memberof
-------------- --------- ------- --------
test test test {}
What my issue now is how to log this to either a csv/text file which can be easily imported back for rollback.
Im currently using clixml which preserves the export ive done with a hashtable within a hashtable.
like:
$user=#{
#{
Username=$user.samaccountname
Memberof=$user.memberOf
}
}
This allows me to more or less use dot notation to access the information stored for easy rollback, but viewing the file is not that easy readable
Which method do you propose me to use to log the info i need when alot of the info is some kind of collection of items?
I've tried to explore with PSobject but i havent gotten the hang of that yet.
Are there any logging method which will output to a easy to read log and easy use for rollback?
If anyone is able to supply an example and also point me to a page to further explore this i will appreciate it very much.
By readerfriendly im thinking of something like this:
Username,Givenname,Surname,Memberof
Test,test2,test3,Admins;somegroup;somegroup
or
Username,Givenname,Surname,Memberof
Test,test2,test3,Admins somegroup somegroup
EDIT 1
Here is the code that gets the info:
Get-ADUser -Identity test -Properties SamAccountName,GivenName,Surname,Memberof |
Select-Object -Property SamAccountName,GivenName,Surname,Memberof | export-csv C:\test.txt
This is what is in the file as output:
#TYPE Selected.Microsoft.ActiveDirectory.Management.ADUser
"SamAccountName","GivenName","Surname","Memberof"
"test","Mari","Hopkins","Microsoft.ActiveDirectory.Management.ADPropertyValueCollection"
You already have this information primed for a CSV. Why not just use Export-CSV and the opposite Import-CSV
$test2 | Export-CSV -Path c:\temp\backup.csv -Append
Append will add the item to the file and not overwrite the current contents. Then, if you need to, you can import it back with Import-CSV. Likely with multiple objects
$deprecatedAccounts = Import-CSV -Path c:\temp\backup.csv
To create the output you could collect the users with an array.
$users = #()
$users += $test2
$users | Export-CSV -Path c:\temp\backup.csv -Append
Edit from comments
Sorry, I didnt understand the issue at first. What you need to do is expand MemberOf from an array to a string.
Get-ADUser test -Properties SamAccountName,GivenName,Surname,Memberof |
Select-Object SamAccountName,GivenName,Surname,#{Label="MemberOf";Expression = {$_.Memberof -join ";"}} |
Export-Csv -Path c:\temp\backup.csv -Append -NoTypeInformation
Create a calculated property in the select-object that expands the memberof into a semicolon delimeted string. -NoTypeInformation removes the #TYPE Selected.Microsoft.ActiveDirectory.Management.ADUser line if you do not like that.
If you ever needed to process this back into an array you could do so with a simple split
Import-CSV c:\temp\backup.csv | ForEach-Object{$_.MemberOf -split ";"}
Depends on what you mean by easy to read, but how about using Export-Clixml to store the object and Import-Clixml to import it?

Powershell script:how to Filter on AD Get commands to return certain information

I am trying to create a powershell script that checks AD group membership/domain/manage by etc amongst other information and puts into csv file, because I want to structure the csv file in a certain way how do i filter within the actual script to only return certain information e.g. withe the code below its returning a lot more columns but from these the only ones i want are "managed by" and "name":
Get-ADDomain -property managed By, Name|Export-csv -path C:\AD\Domain.csv -NoTypeInformation
Use -Properties to specify the properties you want, and then pipe to Select-Object to select the properties you want in the output. For example:
get-aduser -filter * -properties canonicalName,userPrincipalName |
select-object canonicalName,userPrincipalName |
export-csv myfile.csv -notypeinformation
Get-ADDomain doesn't have -Properties but you can still use Select-Object:
get-addomain | select-object ManagedBy,Name | export-csv myfile.csv -notypeinformation

Create .txt file inPowesShell containing Version number for all files in folder

Good afternoon,
Although I've been programming in VBA for 20 years, I'm on Week 2 with PowerShell, so please be patient!
I want to use a PowerShell script to create a txt file containing details for all .dll files greater than 15000 bytes within the specfied folder thus:
$MySourceFolderName = "C:\PetesStuff\01 Backup"
$MyOutputFile = "C:\PSOutputA.txt"
get-childitem $MySourceFolderName -recurse |where-object {$_.length -gt 15000} | where-object {$_.extension -eq ".dll"} |
sort-object -property Length -descending | Format-Table Name, Length -auto| Out-File -filepath $MyOutputFile
So far, this works, but I also want to include the File version, which can't be accessed in the same way that Name and Length can.
Can anyone help me out, please?
Thanks in advance
Pete
Ok, can do! What you want is Get-Command (I don't know why, but I do know it works). We can put it in a little impromptu hash table in the Format-Table command. Also, I'm going to move the second Where statement to a -filter so the provider filters that for you, and toss in some aliases because this is such a very long line.
GCI $MySourceFolderName -recurse -Filter "*.dll" | ?{$_.length -gt 15000} | sort -property Length -descending |
FT Name, Length, #{l="File Version";e={(Get-Command $_.FullName).FileVersionInfo.FileVersion}} -auto| Out-File -filepath $MyOutputFile

PowerShell Out-file manipulation

i hope someone can help.
I am trying to manipulate a file created by powershell.
I managed to get to the end result that i want, but i am sure it would be easier if it was only one command.
# Invoke the Exchange Snapping ( make sure you are Exchange Admin to do it SO)
add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010
#Create a file with list of DL in the organization
Get-DistributionGroup | Select-Object Name | Out-File C:\Pre_DLGroups.txt
$content = Get-Content C:\Pre_DLGroups.txt
#Remove the 3 first lines of the file that you dont need it
$content | Select-Object -Skip 3 | Out-file C:\DLGroups.txt
#Trim the space in the end and crate the Final file
Get-Content C:\DLGroups.txt | Foreach {$_.TrimEnd()} | Set-Content c:\FinalDLGroup.txt
is that way to make the end result in a single file rather than creating 3?
cheers
Elton
You can send your content across the pipeline without writing it out to files. You can use parenthesis to group the output of certain sets of cmdlets and/or functions, and then pipe that output through to the intended cmdlets.
This can all be applied on a single line, but I've written it here on multiple lines for formatting reasons. The addition of Out-String is something of a safety measure to ensure that whatever output you're intending to trim can actually be trimmed.
Since we're not getting this content from a text file anymore, powershell could possibly return an object that doesn't understand TrimEnd(), so we need to be ready for that.
(Get-DistributionGroup | Select-Object Name) |
Out-String |
Select-Object -Skip 3 |
Foreach {$_.TrimEnd()} |
Set-Content c:\FinalDLGroup.txt
However, an even smaller solution would involve just pulling each name and manipulating it directly. I'm using % here as an alias for Foreach-Object. This example uses Get-ChildItem, where I have some files named test in my current directory:
(Get-ChildItem test*) |
% { $_.Name.TrimEnd() } |
Set-Content c:\output.txt
Get-DistributionGroup |
Select-Object -ExpandProperty Name -Skip 3 |
Set-Content c:\FinalDLGroup.txt