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

Out-File Blank

$
0
0

How do I fix this so the information is in the text file? The text file is created, but has no info in it. I know I am not sending the data to the file, but I am not sure what to do to.

Get-LocalUser | Foreach-Object {

    $user = $_
    $lastlogon = Get-LocalUser | Where-Object {$_.lastlogon}
    $date = Get-Date


    New-Object -TypeName PSObject -Property @{
        Date = $Date
        Computer = $env:COMPUTERNAME
        Name = $user.Name
        Last_Logon = $user.LastLogon

    }

}

$obj | out-file "c:\users\1178421472E\desktop\USERLastLogon $(Get-Date -f yyyyMMdd).txt"


Modernize password script

$
0
0

I have a wireless SSID that has its password changed daily, to do this I run a basic .bat script which also calls a .vbs to create and format a Word document with the days password. It used to run beautifully on Server 2008 and older as a scheduled task but now it doesn't. Instead of trying to troubleshoot the script and try to figure out why it no longer creates the files, I thought I would modernize it to Powershell. Problem is Powershell is still a foreign language to me that I don't speak. I do know that I should be able to combine both scripts into one powershell script due to how advanced powershell is, I just don't know how and where to start. Any help would be appreciated.
Basically this is how it goes.

1) Run batch script to change domain user account password
2) Write that password into a txt file with WiFi connection...

Powershell Compress-Archive.Out of Memory . Set Max Memory? Garbage Collection?

$
0
0

I have a problem in that my PowerShell Archive archive script crashes after consuming my entire machines memory using the Compress-Archive cmdlet. The for loop added below really helped by only dealing with a few files at a time, but I am still getting "Out of Memory" errors on my large files I am trying to compress.

Is setting max PowerShell memory still an option? I understand set max memory may not work. Also, I see garbage collection can be a solution. Where would one place the garbage collection and what would the appropriate syntax be?

Text
[System.GC]::Collect() 
Text
$ArchiveContents = Get-ChildItem -Path C:\OneDrive | Sort-Object -Descending for ($i=0; $i -le $ArchiveContents.Count; $i=$i+10) { Compress-Archive -Path $ArchiveContents.FullName[$i..($i+9)] -Update -DestinationPath \\Server011\Backup_Archive\CompanyData\Daily...

Shorten This Code

$
0
0

So I've got a script testing a few program by starting them, checking that they run and then closing them. So here's my code so far:

Powershell
functiontestApps{$arrApps,$arrFriendlyApps=getArrApps$i=0foreach($itemin$arrApps){Start-Process$item-ErrorActionSilentlyContinue$shortItem=getShortItem$itemStart-Sleep2if(!(Get-Process-Name$shortItem-ErrorActionSilentlyContinue)){$errorMessage="Software Test: $item failed to launch"pauseScript$errorMessage}else{$crntFrApp=$arrFriendlyApps[$i]Write-Host"$tab Software Test: $crntFrApp launched successfully"|Add-Content$outLogPath-ErrorActionSilentlyContinueStop-Process-Name$shortItem}$i++}}#works out which programs need to be tested and returns an array with the exe names and one with user friendly namesfunctiongetArrApps{$retValStandard='C:\...

Powershell - Last password change

$
0
0

Hi There all

I am new to this whole Powershell stuff, so please bare with me.

I have been tasked to get a list of all users in our OU, and when their passwords were last changed. I thought this would be simple, but i am wrong. My results are so wonky. I get about 60 results (out of 700+) that tell me when the password was last changed, but the rest of the results are blank. I am very confused about where to go from here. Why are some of the results blank? At first i thought it was users who have disabled accounts, but it is not just them, it is most of the company along side this. There is no common denominator, it is not only admins who have their last changed passwords on there. Not all in the same groups, nothing is similar. It is very strange. The script is below. Hopefully someone can help:

get-aduser -filter * -properties...

create loop to try Set-UserPhoto

$
0
0

Hello all,

im working on a script that creates a new user in the AD. at some point i need to set the user photo but im having problems running it in a consistent manner.

these are the commands im using:

Powershell
Set-UserPhoto-Identity$script:newuser.SamAccountName-PictureData$photo-Preview-Confirm:$False-DomainController$ADServerSet-UserPhoto-Identity$script:newuser.SamAccountName-Save-Confirm:$False-DomainController$ADServer

my problem is that sometimes the command will fail because the mailbox is not fully ready on our exchange servers.

i need some help in creating a loop that will try running the commands and if it fails waits a few more seconds then try again for a max time of 1 minute.

when i use this loop it terminates on first fail...

Powershell
while($timer.elapsed.totalseconds-lt60){Try{Set-UserPhoto...

Powershell script to convert dynamic A records to statc?

$
0
0

Hello, I am looking for a PS script to convert about 1000 dynamic dns entries to static. Any help would be greatly appreciated. Thank You 

Exchange Powershell - How do I export a list of distribution groups?

$
0
0

Hi guys, do any of you know any scripts/commands to export a list of all distrubition groups we have in our exchange? I do not need the list of members in this.

Thanks in advance!   


Powershell- need help renaming multiple computers at the same time in our domain

$
0
0

I'm a powershell novice and need assistance with this script. I already created the CSV file with old name, new name in line 1 and the data of old name, new name starting in line 2. When I try running the script, I run into this error.

Script:

$Credential=Get-Credential
$Computers = Import-CSV -Path "C:\RenameComputer.csv"
ForEach ($comp in $Computers){
Rename-Computer -ComputerName $($comp.oldname) -NewName $($comp.newname) -domaincredential $Credential -force
Restart-Computer $($comp.oldname)
}

Error:

Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At line:2 char:35
+ Rename-Computer -ComputerName $($comp.oldname) -NewName $($comp.n ...
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:)...

Out-Host breaking things

$
0
0

So this will work just fine but as soon as I add "Out-Host" whilst the script still runs and the output is displayed to the console, the "if" part occurs causing my own error message to show. Please note I'm only temporarily using write-host until I can get write-output working.

Powershell
functionscripts{$arrScripts=getArrScriptsforeach($itemin$arrScripts){$scriptPath="$letter\Testing\Scripts\2ndScripts\$item"if(!(Powershell.exe$scriptPath|Out-Host)){$errorMessage="2nd Scripts: $item cannot be executed"pauseScript$errorMessage}else{Write-Host"$tab 2nd Scripts: Starting $item"|Add-Content$outLogPath-ErrorActionSilentlyContinue}}}

Powershell - Folder Watchdog

$
0
0

Looking for some guidance for the best method to watch a specific folder and return a pass fail when checking at if there is a file with the today() date stamp. Does anybody have such a script in their library.

I have the following code snippet that will display the file if the condition is true.

Powershell
$date=Get-Date$date=$date.AddHours(-($date.Hour))$date=$date.AddMinutes(-($date.Minute))get-childitem-Pathc:\pstest-recurse|Where-Object{$_.lastwritetime-gt$date}

Ultimately I am looking to have an e-mail sent with the status of PASS or FAIL if the condition is met.

powershell to run on multiple machines in textfile

$
0
0

hello all,

i am working on script to install msi on different machines

Powershell
$list=get-content"c:\windows\temp\systems.txt"Foreach($systemin$list){if((gwmiwin32_operatingsystem|selectosarchitecture).osarchitecture-eq"64-bit"){msiexec.exe/qn/I"\\server\test.msi}else{ msiexec.exe /qn /I "\\server\test32.msi}}

the script is installing on machine i ma running from. its not taking the test input, am i missing anything?

thanks

New-TimeSpan Start and End Date with NULL Data

$
0
0

So I have a script that is pulling the last logon date for a AD users. However, some of them have never logged in so that field will be empty.

Here is how I have the script portion set up:


Those that have empty fields will produce the following error (these are people with no date in that field.):


How do I deal with those who have no data for the last logon date in AD? I do want to count them as part of those who will get an email but the error is being produced at the New-TimeSpan line and they are not getting the email that they should be getting.

PowerShell Command to send warnings to a file

$
0
0

I am creating a script that will catalog all of the inbox rules for each user in our organization so we can review them to make sure there are not any nefarious ones setup by outside sources. The command I have set so far is: 

Get-InboxRule -Mailbox user1@trademarkproperty.com | Export-Csv -Path C:\Exch_Rules\User1_Rules.csv

When I run this command in powershell I see in the console that some of the rules it pulls have warnings due to errors in them.  I would like to be able to have these warnings captured into a separate file.  Is there a cmdlet I can deploy to make this happen?

Exchange On-Prem Add Mailbox Permissions en masse

$
0
0

Simple way to sweep through all your Exchange 2010 on-prem Mailboxes & set permissions on all.

Suppose you and your boss (and a couple others) want to be able to open any User's Mail. Don't ask, it happens. You don't want to slog through every Mailbox with the Add-MailboxPermission command, but would rather just set them all at once, for all 3 of you snooping spies ().

This will do it. For the "Boss" users Bob, Carol, Ted & Alice:

Powershell
ForEach($mbxinGet-Mailbox){ForEach($bossin'Bob','Carol','Ted','Alice'){Add-MailboxPermission-Identity$mbx.Alias-User$boss-AccessRightsFullAccess-InheritanceTypeAll-Automapping$false}# End For Each Boss}# End For Each Mailbox

If you want to check them all, there's not enough screen real-estate to see everything, so you'll need to redirect your results to a file, thus:

...

Script to access unroutable from a server or win10 box

$
0
0

Kind of an odd question.. We have about 70 locations that vpn into our corporate office and they each have a server or Win10 machine that has 2 nics in it. One nic has a routable gateway address on its stack the other, no gateway. It simply cannot be accessed from corporate network because there is no gateway on the 2nd nic.

Is there a way to run a central powershell script (from corp office) where it could use powershell on the remote machine to look for files in a share on a remote machine that sits on the network with the gateway-less nic?

I realize I can do this locally but Id prefer to centralize the script.

Id apologize for being a n00b here but I read the "read this first" post and it said dont apologize.. :)

To be clear:

Remote pc:

Nic 1= 172.19.22.55

subnet 255.255.255.0

gateway=172.19.22.14

nic 2= 10.5.60.12

subnet 255.255.255.0

...

Processing an array returned by Get-ADuser into CSV

$
0
0

I am trying to export specific attributes of all users in a specific OU. One of the attributes is being stored as an array of decimal values so when I do a:

Get-ADuser -Filter * -SearchBase "OU=test,OU=org,DC=blah,DC=blah" -Properties sAMAccountName, array_attribute | Select sAMAccountName, array_attribute | export-csv myfile.csv

It results in the CSV file containing Microsoft.ActiveDirectory.Management.ADPropertyValueCollection under the array_attribute column, because export-csv cannot handle arrays.

I need to be able to import the result of Get-ADuser into Excel so, does anyone know of a way to accomplish this?

365 powershell MFA to multiple services

$
0
0

I'm assuming I know the answer, but I will kick myself if wrong :)

Historically, I have scripts that connect to 365 exchange, Azures-AD and msol-service.  And I grabbed credentials in a string and passed them to each service, so typed in username/password once.

Now with MFA, I assume I need to type in username/password and 2FA approval for each connection (so 3x).

Before I wear out my keyboard, is that the only way, or can you grab credentials with approved 2fa and pass to these connections?

Thanks

Add ad users in bulk

$
0
0

First I made a script for making OU in my AD, that worked fine

A txt file:

Text
_organisatie _organisatie\Directie _organisatie\IT _organisatie\Aankoop _organisatie\Verkoop _organisatie\Support _shares _shares\Directie _shares\IT _shares\Aankoop _shares\Verkoop _shares\Support 

OU Script:

Powershell
Get-ContentC:\OUStructure.txt|Foreach-Object{$domain='DC=Lcorp,DC=local'$allOU=''$ous=(Split-Path$_-Parent).Split('\')[array]::Reverse($ous)$ous|Foreach-Object{if($_.Length-eq0){return}$allOU=$allOU+'OU='+$_+','}$allOU+=$domain$NewOU=Split-Path$_-LeafNew-ADOrganizationalUnit-Name"$NewOU"-Path"$allOU"-ProtectedFromAccidentalDeletion$false}

No i want to populate these OU with users, so i mad a csv

CSV:

Text
firstname,lastname,username,password,ou...

Power shell screep

$
0
0

Hello!

Can maybe someone help me?

I need PowerShell scrip who will check the specified directory for file who have in name *Subline* and created today if file founds retur 1 if not 0

Viewing all 15370 articles
Browse latest View live


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