Quantcast
Channel: PowerShell
Viewing all 15370 articles
Browse latest View live

Comparing a CSV against Active Directory - Powershell not working right

$
0
0

Hi All! I've been working on this script for quite a while and I seem to have run into a roadblock where I just cannot figure out what is going wrong. It's likely something to do with my $Userfound statement but I'm not sure how to fix it, so I am turning to the Spiceworks community to help. I am relatively new to Powershell so please keep that in mind.

Powershell
$path=Read-Host-Prompt'Input full file path and name of the file you wish to analyze'$foundenabled=New-ObjectSystem.Collections.ArrayList$founddisabled=New-ObjectSystem.Collections.ArrayList$notfound=New-ObjectSystem.Collections.ArrayListImport-Csv$path|Foreach-Object{try{$UserFound=Get-ADUser-Identity$_.UserName-PropertiesipPhone,EmailAddress-ErrorActionStop}catch{}if($UserFound){if($UserFound.Enabled-eq$true){$foundenabled.Add((...

PowerShell - Folder Variable Does Not Change

$
0
0

Hi all,

Consider the following code:

Powershell
$Failures=@()$Folder=1If($Files){Write-Output"The files/folders to be excluded are: `r`n"Write-Output$ExclusionWrite-Output"`r`n"Write-output"The files/folders to be removed are: `r`n"Write-Output$FilesWrite-Output"`r`n"Foreach($Filein$Files){Write-Debug$FolderWrite-Output"Renaming $File to $Folder"Try{Rename-Item$File-NewName$Folder-Force-ErrorActionStop$File="C:\LDriveBuilds\$Folder"}Catch{Write-Error$_.Exception.MessageContinue}Write-Output"Now removing files within $File. `r`n"Try{Get-ChildItem$File-Recurse|Where-Object{!$_.PSIsContainer}|Remove-Item-Force}Catch{Write-Error$_.Exception.Message$Failures+=$_.Exception.ItemName$Folder=$Folder++Continue}Write-Output"Now removing $File. `r`n"Try{Remove-Item$File...

Guide to enabling/configuring Bitlocker via Group Policy (in a domain)

$
0
0

Since I cannot seem to find a single guide that fully shows me how to set up and configure Bitlocker in a domain with recovery keys backed up, I thought I would put this guide here to try and save others some time if they ever find themselves in a similar position.

So... Let's say you have an environment with 150+ employees. All their laptops (which are company owned/managed) have previously not had any Bitlocker on. They were all imaged via an ImageX/WDS deployment using Microsoft Deployment Toolkit.

Later on, you want to roll out Bitlocker to all these laptops, ideally with minimal physical intervention. Group policy sounds like the way!

One of the first things you notice when looking at the group policy options is... There's no 'Enable Bitlocker' policy. You can specify the parameters but not actually turn it on... This is where the...

PowerShell trick at the Console - select from your named parameters easily

$
0
0

I'm not sure if anyone has seen this yet, but I just discovered it today, and it kinda gave me a small 'mind-blown' moment. Sorry if this is repeated but...

Using tab-completion in the scripting environment

Everyone who has worked in the ISE, VSCode, or any other PowerShell-aware scripting environment is aware of tab-completion. You type some code, hit [Tab] and the ISE will show you a list of viable items that you might be looking for.

In the console, too

The console does this too. Keep on hitting [Tab] and you'll move through the available items which might complete the bit of code you started.

Selecting from a list of auto-complete options in the console!

Now, did you know that if you run the straight console, not the ISE, you can also get a really cool list of predicted objects/cmdlets, etc., from which you can navigate through using...

Finding folder permissions for specific users listed in a text file

$
0
0

I am currently using Powershell to pull information about access for certain users which are listed in a text file (each username being on a separate line).

I have managed to use the code below to pull information about mailboxes and used similar code to get information about User Groups in Active Directory.

Powershell
$start=Write-Host"[START] Creating combined record of all users"$users=ForEach($userin$(Get-ContentC:\Files\user_audit.txt)){Write-Verbose"Creating record for $user"-verboseGet-Mailbox|Get-ADPermission-User$user|Where{($_.ExtendedRights-like"*Send-As*")}|Select@{L='User';E={$user}},Identity,Deny}$start$users|Export-csvC:\Files\Mailbox_SendAs.csv-notypeinformationif($Host.Name-eq"ConsoleHost"){Write-Host"[FINISHED] Combined record created"}

I'm now having problems finding what...

Updating an a existing csv with data from AD

$
0
0

Using this script from JitnSh I am able to loop through a csvand determine what security groups the user is a member of.

Powershell
$imported=Import-CsvE:\bin\Active020119.csv$output=foreach($iin$imported){Get-ADUser$i.username-PropertiesDisplayName,memberof|Foreach{New-ObjectPSObject-Property@{UserName=$_.DisplayNameGroups=($_.memberof|Get-ADGroup|Select-ExpandPropertyName)-join","}}$output|SelectUserName,Groups|Sort-Object-PropertyName|Export-CsvC:\results-NoTypeInformation

I want to take this a bit further and check.

1) does the user exist in a specific security group

2) And if that is YES then write YES back a new column in the original csv.

I was attempting to do this with -InputObject in the Export-Csvbut I cant do that when using a pipe to Export-Csv

WinRM/Kerberos - Test-CauSetup -Hostname Failing -FQDN Working

$
0
0

When I run Test-CauSetup -ClusterName "HVClstr" -Credential $cred (Tried this with and without explicate credentials) the below appears

WARNING: Connecting to remote server HV1 failed with the following error message : WinRM cannot process the request. The following error occurred while using Kerberos authentication: Cannot find the computer HV1. Verify that the computer exists on the network and that the name provided is spelled correctly.

When I use "Enter-PSSession" and connect via hostname it fails but when I use the FQDN it works.

When I enable CredSSP and connect via "Enter-PSSession" it works both ways but CAU does not.

Is there anyway to have the CAU test use the FQDN instead of the host name or am I missing something else in my config?

What I have tried:

CredSSP on one host as a test

Set AD delegation between the 2 hosts and the...

CimInstance > WMIObject

$
0
0

So I've discovered that WMI is going to be deprecated so I'm trying to convert my old uses of Get-WMIObject to Get-CimInstance. The issue is, for example, if I try this:

Powershell
$usbDrive=Get-WMIObjectwin32_diskdrive|Where-Object{$_.interfacetype-eq"USB"}|ForEach-Object{Get-WmiObject-Query"ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"}|ForEach-Object{Get-WmiObject-Query"ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"}|ForEach-Object{$_.deviceid}

This basically just gets the drive letter of any USB device connected and if I were to swap out every "WmiObject" with "CimInstance", it will still work. I really am not sure if I should rely on it just working or if...


Powershell foreach on multiple csv headers

$
0
0

Hello,

Can someone help with the below; -

I'm using PowerShell to run through a list of users in multiple security groups from a CSV file. Each security group is listed as the "header" in the CSV file and then the users that are supposed to be in them groups are listed underneath each header.

How can I run through each header and process each user that's underneath that header?

I've tried importing a txt that contains all the group names and then running a foreach loop through the txt file, but then when I use another foreach loop underneath for the users, it will only pull back the first header name.

Powershell - get-ad* with filter

$
0
0

Hey all,

I'm trying to query AD and get a list of users in a certain group and starting with certain letters in the description.

This is my first attempt at powershell to solve anything and I feel like I'm close but am missing something.

I've used this to get the description part and it works fine, but I cannot find how to add a filter for a group that they are a member of.

Powershell
get-aduser-filter{(Description-like"abc*")}

This did not work, it gave me a after entering.

Powershell
get-aduser-filter{(Description-like"abc*")-and(memberof-like"group")}

Also tried this.

Powershell
get-aduser-filter'Description -like "abc*",MemberOf -like "group"'

I only want to query members in a certain group that start with abc in the description, it's driving me nuts here, any help is greatly appreciated.

Powershell CSV data without specifying headers as variable

$
0
0

Hi,

Is there anyway way to do the following; -

Import CSV

Grab all header names from CSV

Count how many items are in each column

Run a foreach loop on column based on the count

Basically, I have a large CSV containing Active Directory security groups, and then under each header are the users that are in meant to be in the security groups.

I have tried first importing the CSV and then trying to use Get-Member to pull back the header names and filter each column based on that, but this will only bring back the first header name

Then I tried importing the headers first, using a foreach loop on that and then bringing the CSV into the loop and trying to filter the headers based on this. But this hasn't worked either.

Then I tried running a foreach loop on the headers, and then adding a nested foreach loop to bring in the csv file so I could match...

find UserPrincipalNameby imprting user firstnames from csv

$
0
0

Hello all 

i have a csv file with first name last name etc i am trying to use the first name to get the users upn NOTE the upn is not listed in the csv i want to query form the AD but if i run i get error the identity is null can anyone have any idea 

thanks in advance 

Import-Module ActiveDirectory
$names = Import-Csv "c:\load.csv"
$names = foreach ($Firstname in $names) {
$name.DisplayName
Get-ADUser -Identity $name.DisplayName | select UserPrincipalName
}

Problem with If/else statement.

$
0
0

New to power shell and for the life of me can't figure out what I'm doing wrong with my if/else statement. 

Powershell script to find all DL's that three users are part of.

$
0
0

So this is my first time posting here, after a long time lurking, and I am hoping someone can help me out. I  have been asked to add as new user to any O365 distribution lists that three specific users are a part of. So for example, I want to add Bill to any DL's that Tom,Dick and Harry are all members of. They are each on about 40 DL's but all three are only on a handful. Thanks in advance for your help and hopefully I made my question clear!

Thanks!  

PS code for on Prem Exchange 2010

$
0
0

I am new to PS, where do I input my user's email and date in below's script?? I am wanting accurate Exchange 2010, Sent and Received email totals from 10/1/2018 to 01/31/2019..

$info = get-mailbox -ResultSize Unlimited |
foreach{
$received = (Get-MessageTrace -RecipientAddress $_.UserPrincipalName -StartDate "$((get-date).ToShortDateString()) 00:00:00" -EndDate "$((get-date).ToShortDateString()) 23:59:59").count
$send = (Get-MessageTrace -SenderAddress $_.UserPrincipalName -StartDate "$((get-date).ToShortDateString()) 00:00:00" -EndDate "$((get-date).ToShortDateString()) 23:59:59").count

new-object psobject -Property @{
UserName = $_.UserPrincipalName
SendCount = $send
ReceiveCount = $received
Date = (get-date).ToShortDateString()
}
}

$info

FYI I have tried the following:

Get-TransportServer | Get-MessageTrackingLog...


Run powershell command

$
0
0

Hi everyone,

I have to run a powershell command in a scheduled task, the command is:

powershell -command "& {Get-Process | out-file d:\tex.txt -append}"

Before to run in scheduled task, I run this command in powershell terminal for test first.

It worked on powershell build version 10.0.17134.407 but for pc running build version  10.0.17134.112, terminal return nothing and the command only worked when left last character ( " ) to the next row. 

In the terminal window will be shown extractly like this:

powershell -command "& {Get-Process | out-file d:\tex.txt -append}

>>"

does anyone have any ideal about this? Tks you

dropping the space in first name

$
0
0
i am able to populate email field in each user by running the below command
Text
Get-ADUser -Filter * -SearchBase 'CN=CN,DC=DC,DC=DC' | ` 
    ForEach-Object { Set-ADUser -EmailAddress ( $_.givenName + '.' + $_.surname + '@email.com') -Identity $_ }
but i start to run into a problem. some of my given name are as follows
Given Name
Jack
Ah Beng
Beng Soon
the list goes on.
Is there a way to run through the Given Name with a script to check if there is a space, drop the space and my final output is
Given Name
Jack
AhBeng
BengSoon
 

Powershell

$
0
0

Hi Friends,
help with Powershell!!!!!

i want to move ADComputer  to OU which has the same name!!! 
Example >

move PC170-01 to ou 170

move PC122 to ou 122 ..... etc

array in variable

$
0
0
Text
$writeaccess = get-itemproperty -path "hklm:SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices\{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}" -name "Deny_Write" $whitelisthostname = hostname if (($writeaccess.Deny_Write -eq 1) -or ($whitelisthostname -eq "hostname1") -or ($whitelisthostname -eq "hostname2") -or ($whitelisthostname -eq "hostname3") -or ($whitelisthostname -eq "hostname4")){ $compliance = 0 }else { $compliance = 1 } echo $compliance 

anyone good in powershell? i have the above powershell script. i would like to whitelist a few hostname but the if else statement will get longer if there is more hostname.

any way to define a list of hostname in $whitelisthostname variable then if else statement will do a check of the array of hostname so that don't need to reinput the hostname into the if else statement.

thanks


Powershell Compare lastwrite and size from two folders and save in csv

$
0
0

Hi I have two folders and both had same number of files . I just want to check both folders are identical and any difference print in a csv file.i.e., lastwrite or size or number of files difference from my master folder.

@martin has suggested, which i liked but cannot figure it out to make this work.

Folder 1 : MasterFolder1

Folder 2: share2

What I have tried so far:

Text
$Share1Path = "c:\MasterFolder1" $Share2Path = "C:\share2" $Share1 = Get-ChildItem $Share1Path $Share2 = Get-ChildItem $Share2Path | Group Name -AsHashTable $Count = 0 ForEach ($File in $Share1) { If ($Share2.ContainsKey($File.Name)) { If ($Share2[$File.Name].LastWriteTime -eq $File.LastWriteTime) { "its ok no change." } } Else { "file is different in size or modified date or more files in share2 than in master folder" } } $Object = [PSCustomObject]@{ Share1 =...
Viewing all 15370 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>