I'm new to powershell and I try to solve a problem I already managed to solve with batch scripting. I know how to set a variable and how to get the date with powershell. But I made a special subprogram for the exact date and time in batch and I don't know how to do that in powershell. Also I'm asking myself if there's an equivalent for %~dpn0 in powershell?
This are the parts of my batch script I want to use in powershell:
set pgm=%~n0
set log=%~dpn0.log
set csv=%~dpn0.csv
set dir=%~dp0users
set txt=%~n0.txt
set fix=%~dpn0.fix
call :loggy I "Program %pgm% started..."
:loggy
set welcomesev=%1
set welcometxt=%~2
set welcomeJJJJ=%date:~6,4%
set welcomeMM=%date:~3,2%
set welcomeDD=%date:~0,2%
set welcomeHR=%time:~0,2%
set welcomeMIN=%time:~3,2%
set welcomeSEC=%time:~6,2%
set welcomeDT=%welcomeJJJJ%.%welcomeMM%.%welcomeDD% %welcomeHR%:%welcomeMIN%:%welcomeSEC%
echo %welcomeDT% [%welcomesev%] %welcometxt% 1>>%log%
goto :EOF
Sorry for the many questions but I couldn't find anything with the search function.
Greetings
Does this function help you?
function Extract-CurrentDate{
$date = (Get-Date -Format d).ToString()
$time = (Get-Date -Format t).ToString()
Write-Host "$date $time"
}
Extract-CurrentDate
In generell you can use the Cmdlet Get-Date for you issue. It has many options, see this article. Moreover, you can format your output like this.
Related
Ok I am new but I didn't see any other questions like this.
All I am trying to do is make PS say "Some kinda greeting $date" when I open it.
First I just put Get-Date where $date is but that looped forever. Then I tried
$date = Get-Date
Write-Host Welcome to powershell, today is $date and that also looped forever.
I read the Write-Host may be an issue so I tried Write-Output and did the exact same thing.
So finally I tried adding a while loop with a counter and it did the exact same thing.
$c=0
While ($c -lt 1){
Write-Host The date is (Get-Date)
$c++
}
This all works if PS is already open or if I use the ISE but loops forever when it is in the profile script.
Thanks for the help!
Thanks to the folks that tried to help! It looks like I copy and pasted replacing one of the paths to a module that I wanted to load with the path to the profile causing the loop.
I have a write-host line that I want to execute a command in the middle of, so the output will be inserted in the middle of the write-host string.
Basically I have a txt file that holds configuration data for a suite of scripts, one of the configurations is the format for dates and time. For example, there is a configuration for the year format which is 'YYYY' and it is written to $Year.
So what I would like to do is something like this:
Write-Host "The year is " Get-Date -Format $Year.ToLower()
What I expect to see on my screen when this is ran, is
The year is 2019
Now I know I can declare another variable with this logic and just have...
Write-Host "The year is $NewVariable"
...but I was hoping not to create another variable. This is a dumb-ed down example of my scrip, so I would be creating a lot of variables if I go this rout. Please note I am using .ToLower() to compensate for the user's input into the configuration text file.
In order to print the year of the Get-Date run this:
Write-Host "The year is $(Get-Date -Format yyyy)"
This way you will always get the year that is generated by Get-Date
This works fine....
$year = "2019"
Write-Host "The year is" (Get-Date -Format $Year.ToLower())
I'm trying to look up a value in PowerShell and paste it as a string into a line of text on another application.
In this instance I need to put the date into a line for an API call. I've found the date and can access the line where I need to input the date. I'm having trouble highlighting the date output, or using the variable where I have stored the date to paste that date into the line of code I'm using.
PowerShell code
$TDay = (Get-date).AddDays(-14)
Get-Date $TDay -Format MM-dd-yyyy
--API call
https:\\companyteam.teamwork.com/desk/v1/tickets/search.json?lastUpdated=07/26/2019
I'm able to delete the previous date but now I need to paste the value of the result from the PowerShell Script into where the date is displayed on the API call.
To solve my problem I think I'll need to manually highlight the results from the API call using the PowerShell mouse commands or if I could potentially just tell PowerShell to paste the result of Get-Date $TDay -Format MM-dd-yyyy
You can use Set-Clipboard to set the clipboard contents, which is the equivalent to a copy. You can use Get-Clipboard to paste copied contents to the console or as a variable value. You can also just paste from any copy-paste menu that has access to your clipboard.
Get-Date $TDay -Format MM-dd-yyyy | Set-Clipboard
$variable = Get-Clipboard
Since I am not sure how you are making the API call, simplistically speaking let's assume the URI is just a string. You can still use the Get-Clipboard command or variable within the sub-expression operator $(). Variables don't technically need the sub-expression unless you are accessing properties of that variable.
"https:\\companyteam.teamwork.com/desk/v1/tickets/search.json?lastUpdated=$(Get-Clipboard)"
# OR
"https:\\companyteam.teamwork.com/desk/v1/tickets/search.json?lastUpdated=$variable"
I'm looking for a bit of assistance here. I currently have a Powershell script which adjusts the dates within a file. I'm looking to stop myself having to manually adjust these dates every time. What I need is to replace the date two days ago, with the date from yesterday.
I believe that I'd have to use (Get-Date).AddDays(-1) and (Get-Date).AddDays(-2) but I'm not exactly sure how I'd script this in!
What I currently have:
echo "Adjusting Import Dates"
(Get-Content D:\Temp\Example.txt).replace('20180917', '20180918') | Set-Content D:\Temp\Example.txt
You could try this:
$yesterday = (Get-Date).AddDays(-1).tostring("yyyyMMdd")
$twodaysago = (Get-Date).AddDays(-2).tostring("yyyyMMdd")
(Get-Content D:\Temp\Example.txt).replace($twodaysago, $yesterday) | Set-Content D:\Temp\Example.txt
You just introduce variables for the two dates and format them to the required date format.
There is probably some other way of replacing in files, but the above should work.
I have a script that I converted to EXE so I can run it as a service. I am trying to make the script execute itself again on some condition. I have a for loop in the script and I want to break and re-execute so it would similar to this:
foreach{
if(Get-Date -format "yyyyMMdd" != $CURRENTDATE){
**re-execute script**
}
send_email($_)
}
Wrap your code into function and in if call this function.
There are a few ways to do this.
You can make a function, and call it directly:
function myFunction($myCollection)
foreach{
if(Get-Date -format "yyyyMMdd" != $CURRENTDATE){
myFunction $myCollection)
}
send_email($_)
}
More info for using functions can be found here (They are odd things compared to C syntax)
Would probably be the optimal route.
Another route would be a while loop:
while(Get-Date -format "yyyyMMdd" != $CURRENTDATE)
{
foreach{
#Iterate foreach here, then exits to loop, where it checks condition again
#If still does not match todays date, it goes through foreach loop again.
}
} #loop
send_email($_) #Script will not get here until it meets your date check condition
If you want to run this as a service you need some way to limit the frequency that the script checks the date, otherwise it'll use a lot of CPU!
There are a couple of ways you could do this. You could take other answers here and add the Start-Sleep cmdlet with a suitable amount of time in the for/while loop.
However I'm thinking you might have a reason to rerun your EXE. The other answers wouldn't rerun the EXE, they would just keep the EXE running until the date rolled around.
If you do want to rerun your EXE you can just enter the EXE's command line where you have **re-execute script**. You won't need the for loop, but you do need to invoke Start-Sleep to something reasonable.