Quantcast
Channel: /r/powershell – How to Code .NET
Viewing all 8793 articles
Browse latest View live

[help] Out-File to network path resulting in path error

$
0
0

Function

function Get-RemoteCompFWRules ( [Parameter(Mandatory=$false, Position=0, ParameterSetName="LiteralPath", ValueFromPipelineByPropertyName=$true, HelpMessage="Literal path to core server passed as a network location (\serverrootdirdir)")] [Alias("CSPath")] [ValidateNotNullOrEmpty()] [string[]] $Core_Server_Save_Location) { $file_name = "$Core_Server_Save_Location" + "$(Get-Date -Format yyyy-MM-dd:HH:MM)" + "${env:COMPUTERNAME}-FWRules.txt" Get-NetFirewallRule * | Format-List * >> $file_name } 

Running this returns

out-file : The given path's format is not supported. At line:1 char:1 + Get-NetFirewallRule * | Format-List * >> $file_name + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OpenError: (:) [Out-File], NotSupportedException + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand 

I feel like this is a simple fix but Im not seeing it.

submitted by /u/toe-fu
[link] [comments]

The post [help] Out-File to network path resulting in path error appeared first on How to Code .NET.


if a variable is less than 8 characters long pad leading 0s till it it’s 8 characters

In Need of some assistance with a script, working but I think it is missing a few things

$
0
0

Hello,

I’m trying to write a script that checks to see if a service exists, if it does, do nothing but if it doesn’t install the service. This is what I have so far and it installs the service but I’m not confident that my if statement is working.

$ErrorActionPreference = 'SilentlyContinue'

If ((Get-Service).Name -eq 'Test') {

} Else {

New-Service -Name Test -DisplayName "Reddit" -Description "Test" -BinaryPathName "C:Reddittest.exe" -StartupType Automatic | Start-Service

}

Any help is much appreciated. Thank you!

submitted by /u/adrzie
[link] [comments]

The post In Need of some assistance with a script, working but I think it is missing a few things appeared first on How to Code .NET.

[help] Emailing from powershell

$
0
0

Good Afternoon r/powershell!

I recently got tasked with sending invoices to ~900 users, prior to me, this was all done manually, but I’m not a fan of copying and pasting that many emails.

I thought I could automate the task with powershell, but I’ve run into a problem – the email I am using sends every invoice to every email in the CSV of $users.

My PS code and sanitized data example are below, could someone take a look and let me know what I’m doing wrong? I’ve googled and bashed my head against this problem for a couple days and I can’t figure it out.

Thanks!

>PS C:usersadmindesktop> $csv = import-csv "test.csv" >> $csv.Number |foreach-object { >> send-mailmessage -from "admin@example.com"` >> -to $csv.Email` >> -subject "Your invoice is ready"` >> -body "This message is to let you know there is an invoice ready for your attention, thanks!"` >> -attachment $csv.attachment` >> -smtpserver mail.example.com >> } 

and my data looks like Name Number Email attachment

George Washington 867-5309 george@geocities.com c:usersadmindesktopgwbill

Abraham Lincoln 555-1234 abe@geocities.com C:usersadmindesktopalbill

John Adams 555-6789 jadams@geocities.com C:usersadmindesktopjabill

submitted by /u/superpenguin38
[link] [comments]

The post [help] Emailing from powershell appeared first on How to Code .NET.

Moving files if the filename contains a keyword in an external text file.

$
0
0

I’m trying to script the following process. If a file in a given folder contains a keyword, which I have in a text file, one keyword per line, then move it to another location. I’m still learning PS so I found a snippet that I thought would be close enough to use but it doesn’t work. No errors, just doesn’t do any sorting. Would anyone be so kind to help me out here?

$List = Get-Content C:UsersDownloadskeywords.txt

$Source = "C:UsersDownloadssource"

$Destination = "C:UsersDownloads$((Get-Date).ToString('yyyy-MM-dd'))destination"

foreach ($file in $List) {

$file_to_move = Get-ChildItem -Path $Source -Filter $record -Recurse -ErrorAction SilentlyContinue -Force | % { $_.FullName}

if ($file_to_move) {

Move-Item $file_to_move $Destination

}

}

submitted by /u/NumerousImportance
[link] [comments]

The post Moving files if the filename contains a keyword in an external text file. appeared first on How to Code .NET.

Sorting help, I think I’m very close

$
0
0

Hi everyone, I’ve been pushing to learn more about powershell and I’m still learning. I was hoping that someone could review the following code and check out line 6 to see if I’m writing this correctly because I’m having issues with it running. If I remove my command, | Sort-Object Name, then everything runs normally. However when I try to get the script to pull from AD and sort by last name (I really don’t care if it’s ascending or descending) then the script fails. My only points of reference are this page and Microsofts powershell page which is surprisingly pretty good.

If I’m doing this wrong can someone help me and tell me what I am doing wrong and how I should write this one little bit of code out?

https://pastebin.com/AHPEyPpR

submitted by /u/escapednut
[link] [comments]

The post Sorting help, I think I’m very close appeared first on How to Code .NET.

Powershell WPF GUI: Custom Image based ProgressBar

Does any one now how to make @{count=32} display just as 32?


What is PowerShell doing in the if-statement: ‘if ($Variable)’?

$
0
0

Let’s say I want to do something only when a certain variable exists.

$Exists = 'Yes' if ($Exists) { Write-Output '$Exists does exist.' } if ($Exists -eq $True) { Write-Output "This won't evaluate." } if ($DoesNotExist) { Write-Output "This shouldn't exist." } 

This will output: $Exists does exist.
(Not my favourite brace-style, but it’s easier to paste into the console.)

What is it that PowerShell is actually doing to evaluate if ( $Exists ) { Write-Output '$Exists does exist.' }?

submitted by /u/DrCubed
[link] [comments]

The post What is PowerShell doing in the if-statement: ‘if ($Variable)’? appeared first on How to Code .NET.

How to remove spaces at the end of filenames in a folder?

$
0
0

Hello! I use the RetroArch emulator which stores boxart, screenshot, and titlescreen thumbnail images for each system within three separate folders. I successfully created a Powershell program that removes the “USA” suffix to the filenames.

cd .Named_Boxarts get-childitem | foreach {Rename-Item $_ $_.Name.Replace("(USA)","")} cd.. cd .Named_Snaps get-childitem | foreach {Rename-Item $_ $_.Name.Replace("(USA)","")} cd.. cd .Named_Titles get-childitem | foreach {Rename-Item $_ $_.Name.Replace("(USA)","")} 

 
However, I now have two issues. There is an inconsistent amount of spaces at the end of the changed filenames, and some files have different suffixes such as (USA, Europe) that are not deleted by my program. How do I go about solving these two problems?

submitted by /u/SmokingJuiceBoxes
[link] [comments]

The post How to remove spaces at the end of filenames in a folder? appeared first on How to Code .NET.

How to write to event log in powershell 6?

$
0
0

Hi all!

I’m trying to write to the application event log using Powershell 6. I’m being told:

Write-EventLog : The term 'Write-EventLog' is not recognized as the name of a cmdlet, function, script file, or operable program. 

Up until just a few minutes ago, I considered myself pretty proficient with powershell. I’m explicitly including Windows.Powershell.Management explicitly, but “Write-EventLog” isn’t even in the get-command output for that module.

Can someone point me in the right direction?

Thank you!

submitted by /u/oneofthetwain
[link] [comments]

The post How to write to event log in powershell 6? appeared first on How to Code .NET.

Path with square bracket and wildcard

$
0
0

I’m working on a script and need to get a path that contains brackets and wildcards. I’ve read a handful of posts on how to handle paths with square brackets. For example this works:

`Get-Item "D:``[root``]usersPC UserAppdata"` 

My problem is that using a wildcard in the path doesn’t work and I need to use wildcards like this:

`Get-Item "d:``[root``]users*Appdata`". 

The need for wildcards also prevents me from using -literalpath I’m trying to do something like this:

 $Path = Get-Item 'd:[root]users*AppDataLocalMicrosoftWindowsRecent' Foreach($dir in $path){ DoSometing } 

Any ideas on how to do this?

submitted by /u/grayfold3d
[link] [comments]

The post Path with square bracket and wildcard appeared first on How to Code .NET.

Join Linux to Active Directory with PowerShell Core

First time posting a blog here. Would love you guys feedback!

Help with Internet Explorer and dialog boxes

$
0
0

I’m trying to get my script to download a file but I keep getting a pop-up that asks me if want to leave the page or stay and it hangs the script. I found an article with a possible solution using WASP but their website doesn’t include documentation on how to install it. Any one have any suggestions?

$IE = new-object -com internetexplorer.application $IE.navigate2("url") $IE.visible=$true while ($IE.Busy -eq $true) { Start-Sleep -Milliseconds 2000; } $link = $IE.Document.getElementsByTagName('a') | ?{$_.textcontent -eq 'Export to CSV'} $link.click() 

submitted by /u/turnupthebassto11
[link] [comments]

The post Help with Internet Explorer and dialog boxes appeared first on How to Code .NET.


Error Catching help with Approval Script

$
0
0

Making a Script in work to save some time getting approval from a Security Group owner, when someone requests access to the group.

It work OK, not great. The idea is to create a draft in Outlook2010 that is auto-filled with SG Manager Email address, Subject entered by Analyst. The body contains the requesting users Office, Dept, Manager, Job title.

It does all this fine, but there is zero error catching so for example if the group name entered by the Analyst is wrong or annoyingly has a space at the end, it retains the information saved in the $managerEmail from last time you run it. (I thought -Like would sort this, it does not)

Also It only works for one user one group one email – Where in reality there could be multiple users requesting access or a user requesting access to multiple groups, i’m looking for a hint in the right direction – would I need to employ a loop of some kind in it to accept multiple inputs and create multiple Email drafts.

It a mash of bits of scripts together with my own extremely novice “code” so I’m sure there are parts that do nothing of value so any cleanup advise would also be appreciated, I’m not sensitive about it so any thoughts please share.

https://pastebin.com/6gKBid3w

submitted by /u/MunsterScot
[link] [comments]

The post Error Catching help with Approval Script appeared first on How to Code .NET.

Could anyone help with not outputting error messages

$
0
0

Hello all. I have some network drives that are mapped by the nt authoritysystem. When I open PowerShell locally or from a remote PSSession, I always get “Attempting to perform the InitializeDefaultDrives operation on the ‘FileSystem’ provider failed’. I have tried a couple of things to subdue this message output but none successfully. Would anyone know a workaround this? Thanks!

submitted by /u/augustus94
[link] [comments]

The post Could anyone help with not outputting error messages appeared first on How to Code .NET.

Easy Two-Tier PKI Deployment With MiniLab PowerShell Module

$
0
0

Wanted to share my solution for automating the creation of Two-Tier PKI on a Domain (i.e. new Root and Subordinate/Intermediate/Issuing Certificate Authority) using a PowerShell Module I’m working on called MiniLab.

I also wanted to take the opportunity to write my first blog post and tell all of the good people on /r/powershell about it 🙂

https://pldmgg.github.io/2018/06/02/MiniLab.html

All criticism is welcome (both about my writing style and my code).

Thanks to the community in general for teaching me so much over the past few years and getting me to the point where I actually feel comfortable starting a blog.

submitted by /u/fourierswager
[link] [comments]

The post Easy Two-Tier PKI Deployment With MiniLab PowerShell Module appeared first on How to Code .NET.

Powershell Tools for Visual Studio Mac

Tried to use a command to hide all files of a certain file type but now I can revert

$
0
0

I don’t know much if anything about how powershell works. Basically I get a lot of .dat file types from torrents and I wanted to hide them. So I opened a powershell command to the directory I wanted and used this command I found on a different forum attrib +h *.dat /s Only now even checking and unchecking the “show hidden items” checkbox in explorer isn’t making them come back. How can I fix revert this and properly do it?

Cheers

submitted by /u/Kirshy123
[link] [comments]

The post Tried to use a command to hide all files of a certain file type but now I can revert appeared first on How to Code .NET.

Viewing all 8793 articles
Browse latest View live