8
0

Anlage des Repos

This commit is contained in:
2024-01-24 16:42:38 +01:00
commit 38d6a271c4
1785 changed files with 3051496 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
function Move-Item2 {
param ($file)
Try {
$file = Get-Item -Path $file -ErrorAction Stop
}
Catch {
write-host "error"
}
IF ($file.Extension) {
write-host "ext"
}
ELSE {
write-host "no ext"
}
}
Move-Item2 -file E:\bwtserver.txt
#copy-item
step-
check-
get-verb

Binary file not shown.

View File

@@ -0,0 +1,98 @@
Function Get-OSVersion-withLogging {
Param (
[Parameter(Position=0,Mandatory=$False,HelpMessage='Choose the Return Type. Name')]
[ValidateSet(Name,)]
[STRING]$QueryType=
) #end param
## Parameter für Zahl oder Name
## Parameter für Version oder Buildnummer
BEGIN {
#Clear Error Variable
$error.clear()
$FunctionName = $((($MyInvocation.MyCommand.Name) -split "\.")[0].ToString())
Write-Host "DEBUG Info - $($FunctionName): Begin Function."
#Checking if "Write-LogFile" Module was loaded
IF (Get-Module -All -Name "Write-LogFile") {
Write-Host "DEBUG Info - $($FunctionName): Write-LogFile - Module exists."
} #end if
ELSE {
Write-Host ""
Write-Host "DEBUG Info - $($FunctionName): Write-LogFile - Module does not exist!"
Write-Host "DEBUG Info - $($FunctionName): Please load the Module and try again, running this Function/Module!"
Write-Host "DEBUG Info - $($FunctionName): Exiting, because of this Issue."
EXIT
} #end else
} #end begin
PROCESS {
#Clear Error Variable
$error.clear()
IF (((Get-WmiObject -class Win32_OperatingSystem).version) -match 6.1) {
write-host "Windows Version: 7 / Server 2008R2 was detected."
Set-Variable -Name OSVersion -Value "Win8.0"
Return $OSVersion
} #end if
ELSEIF (((Get-WmiObject -class Win32_OperatingSystem).version) -match 6.2) {
write-host "Windows Version: 8.0 / Server 20012 was detected."
Set-Variable -Name OSVersion -Value "Win8.0"
Return $OSVersion
} #end if
ELSEIF (((Get-WmiObject -class Win32_OperatingSystem).version) -match 6.3) {
write-host "Windows Version: 8.1 / Server 2012R2 was detected."
Set-Variable -Name OSVersion -Value "Win8.1"
Return $OSVersion
} #end elseif
ELSEIF (((Get-WmiObject -class Win32_OperatingSystem).version) -match 10.0) {
Write-Host "Windows Version: 10 / Server 2016 was detected."
Set-Variable -Name OSVersion -Value "Win10.0"
Return $OSVersion
} #end elseif
ELSE {
Write-Host "Windows Version:" ((Get-WmiObject -class Win32_OperatingSystem).version) "was detected."
Write-Host "ERROR: The Operation-System Version is unknown, cant proceed!"
Write-Host "This Script works only with Windows 8, 8.1 and 10 Versions."
Set-Variable -Name OSVersion -Value "Error"
Return $OSVersion
} #end else
} #end process
END {
Write-Host "DEBUG Info - $($FunctionName): End Function."
} #end end
} #end function

View File

@@ -0,0 +1,58 @@
#'Example for VBS '
#=====================================================
# login current user to current windream database
#=====================================================
# Dim Connect
# Dim Session
# Set Connect = WScript.CreateObject("Windream.WMConnect")
#'=====================================================
# create a session
#=====================================================
# MsgBox "Click OK to create a session. This may take some time."
# On Error Resume Next
# Set Session = WScript.CreateObject("Windream.WMSession")
# lErr = Err.Number if lErr <> 0 then
# MsgBox "Could not create session. Error: 0x" + CStr(Hex(lErr)) + " " + Err.Description
# Err.Clear else
# MsgBox " Session created. Click OK to login." end if
#'=====================================================
#' login session
#'=====================================================
# Connect.LoginSession(Session)
# lErr = Err.Number if lErr <> 0 then
# MsgBox "Login failed. Error: 0x" + CStr(Hex(lErr)) + " " + Err.Description
# Err.Clear end if LoggedIn = Session.aLoggedin
# If LoggedIn Then
# MsgBox "Login ok. " + vbCRLF + vbCRLF + "Click OK to close session"
# Else
# MsgBox "Login failed. " End If
#'=====================================================
#' logout
#'=====================================================
# set session = nothing
cls
$WMEntityDocument = 'WMEntityDocument'
$SourceFile = "e:\test.txt"
$TargetPath = "w:\Digital Data - Geschäftsprozesse\Manually\File\"
Try {
}
Catch {
}
$windreamConnect = New-Object -ComObject "Windream.WMConnect"
$windreamSession = New-Object -ComObject "Windream.WMSession"
$windreamConnect.LoginSession($windreamSession)
#IWMSession6 oIWMSession6 = (IWMSession6)oWMSession;
$WMObject = $windreamSession.GetNewWMObjectFSFromFile($WMEntityDocument,$TargetPath,0,$SourceFile)
$windreamSession.getnew
$test1 = Test-Path -Path "w:\system"
write-host $test1

View File

@@ -0,0 +1,291 @@
Function MoveOrCopy-Item-withLogging {
<#
.SYNOPSIS
Function will copy or move File(s).
.DESCRIPTION
If File already exists in target Path new File will get a Version ~2.
.REQUIREMENT General
PowerShell V2
.REQUIREMENT Assembly
<NONE>
.REQUIREMENT Variables
Datei, Datei1, DateiName, DateiEndung, DateiTest, DateiVersion,ZielPath, MoveorCopy, SourcePath
.REQUIREMENT Variables preSet
VersionSeperator
.REQUIREMENT Functions
Write-Logfile
.VERSION
1.2.1.1 / 17.12.2016
.PARAMETER SourceFile
Give the full Path and Filename to one File.
.PARAMETER DestinationPath
Give the Target Path you want to move or copy the specified File.
.PARAMETER Action
Determine which Action you want to perform <Copy|Move>. Default Value is "Copy".
.PARAMETER NewFileName
Give a String, which get the new Name of the File you are moving.
.PARAMETER NewFileName_prefix
Give a String, which get the new prefix Name of the File you are moving.
.PARAMETER NewFileName_suffix
Give a String, which get the new suffix Name of the File you are moving.
.EXAMPLE
MoveOrCopy-Item-withLogging -SourceFile "E:\Quellpfad\Test.txt" -DestinationPath "E:\Zielpfad" -move_or_copy move
.EXAMPLE
MoveOrCopy-Item-withLogging -SourceFile "E:\Quellpfad\Test.txt" -DestinationPath "E:\Zielpfad" -move_or_copy copy
#>
Param (
[Parameter(Position=0,Mandatory=$True,HelpMessage='Give the full Path and Filename to one File.')]
[ValidateNotNullOrEmpty()]
[String]$SourceFile,
[Parameter(Mandatory=$True,HelpMessage='Give the Target Path you want to move or copy the specified File.')]
[ValidateNotNullOrEmpty()]
[String]$DestinationPath,
[Parameter(Mandatory=$True,HelpMessage='Determine which Action you want to perform <Copy|Move>. Default Value is "Copy".')]
[ValidateSet("copy","move")]
[String]$Action="copy",
[Parameter(Mandatory=$False,HelpMessage='Give a String, which get the new Name of the File you are moving.')]
[ValidateNotNullOrEmpty()]
[String]$NewFileName,
[Parameter(Mandatory=$False,HelpMessage='Give a String, which get the new prefix Name of the File you are moving.')]
[ValidateNotNullOrEmpty()]
[String]$NewFileName_prefix,
[Parameter(Mandatory=$False,HelpMessage='Give a String, which get the new suffix Name of the File you are moving.')]
[ValidateNotNullOrEmpty()]
[String]$NewFileName_suffix
) #end param
#Clear Error Variable
$error.clear()
#Checking if "Write-LogFile" Module was loaded
IF (Get-Module -Name "Write-LogFile") {
$FileTest = Test-Path "$SourceFile" -PathType Leaf
IF ($Filetest -eq 'True') {
Write-Logfile -LogLine ""
$SourcePath = ([System.IO.Path]::GetDirectoryName($SourceFile).ToString())
$FileName = ([System.IO.Path]::GetFileName($SourceFile).ToString())
$FileName_noExt = ([System.IO.Path]::GetFileNameWithoutExtension($SourceFile).ToString())
$FileExtension = ([System.IO.Path]::GetExtension($SourceFile).ToString())
IF (!$VersionSeperator) {
Write-Logfile -LogLine "ERROR: Wrong Function call."
Write-Logfile -LogLine "INFO: Variable VersionSeperator is not valid."
Write-Logfile -LogLine "Application was unplannd terminated."
EXIT
} #end if
IF ($newfilename) {
Write-Logfile -LogLine "New Filename ($newfilename) will replace the old one."
Set-Variable -Name Filename -Value ($newfilename+$FileExtension)
Set-Variable -Name Filename_noExt -Value ($newfilename)
} #end if
IF ($newfilename_prefix) {
Write-Logfile -LogLine "New prefix ($newfilename_prefix) has been set for file."
Set-Variable -Name Filename -Value (($newfilename_prefix+$FileSeperator+$Filename) -replace "$FileSeperator$FileSeperator","$FileSeperator")
Set-Variable -Name Filename_noExt -Value (($newfilename_prefix+$FileSeperator+$Filename_noExt) -replace "$FileSeperator$FileSeperator","$FileSeperator")
} #end if
IF ($newfilename_suffix) {
Write-Logfile -LogLine "New suffix ($newfilename_suffix) has been set for file."
Set-Variable -Name FileName -Value (($Filename -split "$VersionSeperator")[0])
Set-Variable -Name FileName_noExt -Value (($Filename_noExt -split "$VersionSeperator")[0])
Set-Variable -Name Filename -Value (($Filename_noExt+$FileSeperator+$newfilename_suffix+$FileExtension) -replace "$FileSeperator$FileSeperator","$FileSeperator")
Set-Variable -Name Filename_noExt -Value (($Filename_noExt+$FileSeperator+$newfilename_suffix) -replace "$FileSeperator$FileSeperator","$FileSeperator")
} #end if
# Does file already exist in the target directory?
$FileTest = Test-Path "$DestinationPath\$FileName" -PathType Leaf
IF ($Filetest -eq 'True') {
Write-Logfile -LogLine "The File ($Filename) already exists in the target directory, starting Version Check."
[String]$FileNameSplit = ($Filename_noExt -split "$VersionSeperator")
[Int]$FileVersion = $NULL
[Int]$FileVersion = $FileNameSplit[1]
# Has the new file already a Version tag?
IF ($FileVersion -eq 0) {
Write-Host "DEBUG Info: Sourcefile includes no VersionSeperator."
# To skip Version ~1.
$FileVersion++
} #end if
ELSE {
Write-Host "DEBUG Info: Sourcefile includes VersionSeperator."
$FileName_noExt = $FilenameSplit[0]
} #end else
DO {
Write-Host "DEBUG Info: Count file version:" $FileVersion; $FileVersion++
}
WHILE (($Filetest = Test-Path -Path "$DestinationPath\$FileName_noExt$VersionSeperator$FileVersion$FileExtension") -eq 'True')
# code block for the copy or move process
Try {
Set-Variable -Name FileHash_befor -Value (Get-FileHash -Path "$SourceFile" -Algorithm SHA512 | select Hash | Foreach-Object {$_ -replace "@{Hash=", ""} | Foreach-Object {$_ -replace "}", ""})
Set-Variable -Name FileTemp -Value (Copy-Item -Path "$SourceFile" -Destination "$DestinationPath\$Timestamp3`_$FileName" -PassThru -Force)
Write-Logfile -LogLine "The $Action command has been completed."
Set-Variable -Name FileFinal -Value (Rename-Item -Path "$FileTemp" -NewName "$FileName_noExt$VersionSeperator$FileVersion$FileExtension" -PassThru -Force )
Write-Logfile -LogLine "File was renamed to: $FileName_noExt$VersionSeperator$FileVersion$FileExtension, and is now transferd to: $DestinationPath."
Set-Variable -Name FileHash_after -Value (Get-FileHash -Path "$FileFinal" -Algorithm SHA512 | select Hash | Foreach-Object {$_ -replace "@{Hash=", ""} | Foreach-Object {$_ -replace "}", ""})
Write-Host "DEBUG Info: Hash Value before: $FileHash_befor"
Write-Host "DEBUG Info: Hash Value after: $FileHash_after"
IF ($Action -eq 'move') {
Write-Host "DEBUG Info: Moving action was choosen, will delete source file."
IF ($FileHash_befor -eq $FileHash_after) {
Try {
Remove-Item -Path "$SourceFile" -Force
} #end try
Catch {
Write-Logfile -LogLine "Error removing the source file."
} #end catch
} #end if
ELSE {
Write-Logfile -LogLine "HASH Value does mismatch!"
Write-Logfile -LogLine "Aborting delete operation!"
} #end else
} #end if
Return $FileFinal.Fullname
} #end try
Catch {
Write-Logfile -LogLine "Error at $Action command, processing file: $SourceFile"
Write-Logfile -LogLine "Please check your privileges"
exit
} #end catch
} #end if
ELSE {
Set-Variable -Name FileHash_befor -Value (Get-FileHash -Path "$SourceFile" -Algorithm SHA512 | select Hash | Foreach-Object {$_ -replace "@{Hash=", ""} | Foreach-Object {$_ -replace "}", ""})
Set-Variable -Name FileFinal -Value (Copy-Item -Path "$SourceFile" -Destination "$DestinationPath\$FileName" -PassThru -Force)
Set-Variable -Name FileHash_after -Value (Get-FileHash -Path "$FileFinal" -Algorithm SHA512 | select Hash | Foreach-Object {$_ -replace "@{Hash=", ""} | Foreach-Object {$_ -replace "}", ""})
Write-Logfile -LogLine "The $Action command has been completed for file: $SourceFile"
Write-Logfile -LogLine "And is now transferd to: $DestinationPath"
Write-Host "DEBUG Info: Hash Value before: $FileHash_befor"
Write-Host "DEBUG Info: Hash Value after: $FileHash_after"
IF ($Action -eq 'move') {
Write-Host "DEBUG Info: Moving action was choosen, will delete source file."
IF ($FileHash_befor -eq $FileHash_after) {
Try {
Remove-Item -Path "$SourceFile" -Force
} #end try
Catch {
Write-Logfile -LogLine "Error removing the source file."
} #end catch
} #end if
ELSE {
Write-Logfile -LogLine "HASH Value does mismatch!"
Write-Logfile -LogLine "Aborting delete operation!"
} #end else
} #end if
ELSEIF ($Action -eq 'copy') {
Write-Host "DEBUG Info: Coping action was choosen, will not delete original file."
} #end elseif
Return $FileFinal.Fullname
} #end else
} #end if
ELSE {
Write-LogFile -LogLine "Cannot find Source File!"
Return $False
} #end else
} #end if
ELSE {
Write-Host ""
Write-Host "DEBUG Info: Write-LogFile - Module does not exist!"
Write-Host "DEBUG Info: Please load the Module and try again, running this Function/Module!"
Write-Host "DEBUG Info: Exiting, because of this Issue."
EXIT
} #end else
} #end function

Binary file not shown.

View File

@@ -0,0 +1,71 @@
Import-Module -Force "E:\Skriptentwickung\released-and-published\Modules\Write-LogFile.psm1"
Import-Module -Force "E:\Skriptentwickung\released-and-published\Modules\Test-FileState-withLogging.psm1"
Import-Module -Force "E:\Skriptentwickung\development\Modules\Test-ItemVersion-withLogging.psm1"
$Error.Clear()
cls
test-ItemVersion-withLogging -DestinationFile test1.txt -Destinationpath E:\
exit
Function Rename-Item-withLogging {
Param (
[Parameter(Mandatory=$True,HelpMessage='Give the Full Path, where the Item(s) located.')]
[ValidateNotNullOrEmpty()]
[String]$Path,
[Parameter(Mandatory=$True,HelpMessage='Give the new Name, you want to set.')]
[ValidateNotNullOrEmpty()]
[String]$NewName
) #end param
###modul loaded??
Write-Host "Path:"
Write-Host $Path
Try {
$Item = Get-Item -Path $Path -ErrorAction Stop
} #end try
Catch {
Write-Host $Error
} #end catch
$FileState = Test-FileState-withLogging -SourceFile $Item -Action writetest
IF ($FileState -eq "writeable") {
Write-Host "SourceFile seems to be writeable!"
Try {
Rename-Item -Path $Item -NewName $NewName -ErrorAction Stop
} #end try
Catch {
Write-Host "Error"
} #end catch
} #end if
return $item
} #end function
$item = rename-Item-withLogging -path "e:\test.txt" -newname test1.txt

View File

@@ -0,0 +1,17 @@
Remove-Variable * -ErrorAction SilentlyContinue
cls
$Error.Clear()
$secpasswd = ConvertTo-SecureString "<password>" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential("marvin@fam-kamm.de", $secpasswd)
Try {
Send-MailMessage -From "me@you.we" -To "marvin@fam-kamm.de" -Subject "test1" -SmtpServer "smtp.strato.de" -Port 25 -UseSsl:$true -Credential $mycreds -encoding ([System.Text.Encoding]::UTF8) -ErrorAction Stop
} #end try
Catch {
$Error
} #end catch

View File

@@ -0,0 +1,135 @@
Function Set-windreamIndex-withLogging {
<#
.SYNOPSIS
.DESCRIPTION
.REQUIREMENT General
PowerShell V3
.REQUIREMENT Assembly
.REQUIREMENT Variables
.REQUIREMENT Variables preSet
.REQUIREMENT Functions
Write-LogFile, Start-windreamSession-withLogging
.VERSION
1.0.0.0 / 20.01.2017
.PARAMETER ModuleName
.PARAMETER ModuleOverrideSourcePath
.EXAMPLE
.EXAMPLE
#>
[cmdletbinding()]
Param (
[Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$True,HelpMessage='Give the ModuleName, you want to load into the current PSSession (without File-Extension)')]
[ValidateNotNullOrEmpty()]
[Object]$windreamSession,
[Parameter(Position=1,Mandatory=$True,ValueFromPipeline=$True,HelpMessage='Give the ModuleName, you want to load into the current PSSession (without File-Extension)')]
[ValidateNotNullOrEmpty()]
[String]$windreamFile,
[Parameter(Position=2,Mandatory=$True,ValueFromPipeline=$True,HelpMessage='Give the ModuleName, you want to load into the current PSSession (without File-Extension)')]
[ValidateNotNullOrEmpty()]
[String]$windreamIndex,
[Parameter(Position=3,Mandatory=$True,ValueFromPipeline=$True,HelpMessage='WMEntityDocumentAndMap = 0, WMEntityDocument = 1, WMEntityFolder = 2')]
[ValidateSet(0,1,2)]
[Int]$windreamIndexEntity,
[Parameter(Position=4,Mandatory=$True,ValueFromPipeline=$True,HelpMessage='Give the ModuleName, you want to load into the current PSSession (without File-Extension)')]
[ValidateNotNullOrEmpty()]
[Array]$windreamIndexValue
) #end param
#Clear Error Variable
$error.clear()
#Checking if "Write-LogFile" Module was loaded
IF (Get-Module -Name "Write-LogFile") {
Write-Host "DEBUG Info: Write-LogFile - Module exists."
IF (Get-Module -Name "Start-windreamSession-withLogging") {
Write-Host "DEBUG Info: Start-windreamSession-withLogging - Module exists."
#[String]$File1 = "\Projekt01 - Geschäftsprozesse\Abnahmeprotokoll\Ap-126506.pdf"
$windreamFSObject = $windreamSession.GetWMObjectByPath($WMEntityDocument, [String]$File1)
IF ($windreamFSObject.aLocked -eq $True) {
Write-Logfile -LogLine "File is locked."
} #end if
ELSEIF ($windreamFSObject.aLocked -eq $false) {
Write-Logfile -LogLine "File is unlocked."
$windreamFSObject.lock()
$windreamFSObject.SetVariableValue("String 09","Herbert5")
$windreamFSObject.save()
$windreamFSObject.unlock()
$windreamFSObject.aID
} #end elseif
ELSE {
Write-Logfile -LogLine ""
Write-Logfile -LogLine "Unexpected Error!"
} #end else
} #end if
ELSE {
Write-Host ""
Write-Host "DEBUG Info: Start-windreamSession-withLogging - Module does not exist!"
Write-Host "DEBUG Info: Please load the Module and try again, running this Function/Module!"
Write-Host "DEBUG Info: Exiting, because of this Issue."
EXIT
} #end else
} #end if
ELSE {
Write-Host ""
Write-Host "DEBUG Info: Write-LogFile - Module does not exist!"
Write-Host "DEBUG Info: Please load the Module and try again, running this Function/Module!"
Write-Host "DEBUG Info: Exiting, because of this Issue."
EXIT
} #end else
} #end function

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,114 @@
Function Test-Path-withLogging {
<#
.SYNOPSIS
Function will check the given Path for existence.
.DESCRIPTION
If Path doesn´t exist, Function will try to create it.
You can disable the create Function by calling with "-Action "ignore"".
.REQUIREMENT General
PowerShell V2
.REQUIREMENT Assembly
<NONE>
.REQUIREMENT Variables
Path, PathTest
.REQUIREMENT Variables preSet
<NONE>
.REQUIREMENT Functions
Write-Logfile
.VERSION
Number: 1.0.1.0 / Date: 17.12.2016
.PARAMETER Path
Give the full Path you want to check or create.
.PARAMETER Action
Determine which Action you want to perform <create|ignore". Default Value is "create".
.EXAMPLE
Test-Path-withLogging -Path "E:\Path\to\create"
.EXAMPLE
Test-Path-withLogging -Path "E:\Path\to\check" -Action "ignore"
#>
Param (
[Parameter(Mandatory=$True,HelpMessage='Give the full Path you want to check.')]
[ValidateNotNullOrEmpty()]
[String]$Path,
[Parameter(Mandatory=$False,HelpMessage='Determine which Action you want to perform <create|ignore". Default Value is "create".')]
[ValidateSet("create","ignore")]
[String]$Action="create"
) #end param
#Checking if "Write-LogFile" Module was loaded
IF (Get-Module -Name "Write-LogFile") {
Write-Logfile -LogLine " "
Write-Logfile -LogLine "Checking Path $Path for existence."
$PathTest = Test-Path -PathType Container $Path
#If given Path already exists
IF ($PathTest -eq "True") {
Write-Logfile -LogLine "Path $Path already exists and can be used."
Return $True
} #end if
#If given Path does not exist
ELSE {
Write-Logfile -LogLine "Path $Path does not exist."
IF ($Action -eq "create") {
Try {
Write-Logfile -LogLine "Path $Path has to been created."
New-Item -Path $Path -ItemType directory -Force -ErrorAction Stop
Return $True
} #end try
Catch {
Write-Logfile -LogLine "ERROR: Unable to create Path."
Write-Logfile -LogLine "INFO: Maybe there is an access or rights Problem."
Return $False
} #end catch
} #end if
ELSE {
Return $False
} #end else
} #end else
} #end if
ELSE {
Write-Host " "
Write-Host "DEBUG Info: Write-LogFile - Module does not exist!"
Write-Host "DEBUG Info: Please load the Module and try again, running this Function/Module!"
Write-Host "DEBUG Info: Exiting, because of this Issue."
EXIT
} #end else
} #end function

View File

@@ -0,0 +1,190 @@
Function Write-LogFile {
<#
.SYNOPSIS
Function will write a given String to a Logfile.
.DESCRIPTION
Just like the cmdlet Write-Host, this Function will display Output on the Console.
Parallel Output will be written into a designated LogFile.
It is recommended to init the LogPath Variable with $Null and the LogPaths Variable with a possible Path,
which has to be checked for accessible.
This is important, because if given Path was inaccessible (-> Access Test failed), Function will try some other Paths for accessible.
These are: The ScriptPath (\Logs) from, which triggerd this Function and $env:temp\Digital Data\$ScriptName\Logs.
After a successful Access Test, Function will set the Variable LogPath, with the first accessible Path tested.
Function will not give any Return Values, because if it is impossible to write a LogFile, Function will force the whole Script to exit.
.REQUIREMENT General
PowerShell V2
.REQUIREMENT Assembly
<NONE>
.REQUIREMENT Variables
PathTest, FileTest, Counter1, LogLine
.REQUIREMENT Variables preSet
LogFile, LogPath, LogPaths, ScriptName, ScriptPath
.REQUIREMENT Functions
<NONE>
.VERSION
Number: 2.0.0.1 / Date: 15.02.2017
.PARAMETER LogLine
Give the String you want to be written into the Logfile.
.EXAMPLE
Write-LogFile -LogLine "Write this in my Log, please."
.EXAMPLE
Write-LogFile -LogLine "Write this Variabel $Variable in my Log, please."
#>
[cmdletbinding()]
Param (
[Parameter(Position=0,Mandatory=$True,HelpMessage='Give the String you want to be written into the Logfile.')]
[AllowEmptyString()]
[String]$LogLine
) #end param
#Clear Error Variable
$error.clear()
# This if / else block is only for determine the LogPath
IF (!$LogPath) {
Write-Host ""
Write-Host "DEBUG Info - Write-LogFile: LogPath is currently not set!"
[Array]$LogPaths = $LogPaths
[Array]$LogPaths = ($LogPaths += "$ScriptPath\Logs","$env:temp\Digital Data\$ScriptName\Logs")
[int]$Counter1 = 0
DO {
#Determine the current Array object
[String]$LogPath = [Array]$($LogPaths[$Counter1])
Write-Host ""
Write-Host "DEBUG Info - Write-LogFile: Testing LogPath: $LogPath"
$PathTest = Test-Path -PathType Container "$LogPath"
#Check if Path already exists
IF ($PathTest -eq $True) {
Write-Host "DEBUG Info - Write-LogFile: LogPath seems already to exists: $LogPath"
} #end if
ELSE {
#If Path doesnt exist, try to create it!
Try {
Write-Host "DEBUG Info - Write-LogFile: Trying to create LogPath: $LogPath"
New-Item -ItemType Directory -Path "$LogPath" -Force -ErrorAction Stop | Out-Null
Write-Host "DEBUG Info - Write-LogFile: Trying seems to be successful!"
} #end try
Catch {
Write-Host "DEBUG Info - Write-LogFile: Cannot create LogPath or access denied!"
Write-Host $Error
} #end catch
} #end else
#Check again if path exists
$PathTest = Test-Path -PathType Container "$LogPath"
IF ($PathTest -eq $True) {
Try {
Write-Host "DEBUG Info - Write-LogFile: Trying to write a TestFile into the LogPath."
New-Item -ItemType File -Path "$LogPath\AccessTest.txt" -Force -ErrorAction Stop | Out-Null
Add-content "$LogPath\AccessTest.txt" -Value "This is a Test!" -Force -ErrorAction Stop | Out-Null
$FileTest = Test-Path -Path "$LogPath\AccessTest.txt" -PathType Leaf -ErrorAction Stop
Remove-Item -Path "$LogPath\AccessTest.txt" -Force -ErrorAction Stop | Out-Null
Write-Host "DEBUG Info - Write-LogFile: Write Test seems to be successful."
Set-Variable -Name LogPath -Value $LogPath -Scope Global -Force
} #end try
Catch {
Write-Host "DEBUG Info - Write-LogFile: Cannot write into LogPath or access denied!"
Write-Host $Error
} #end catch
} #end if
ELSE {
Write-Host "DEBUG Info - Write-LogFile: Cannot create LogPath or access denied!"
Write-Host $Error
} #end else
[int]$Counter1++ | Out-Null
} #end do
UNTIL ((($PathTest -eq $True) -and ($FileTest -eq $True)) -or ($Counter1 -eq $($LogPaths.count)) )
} #end if
ELSEIF ($LogPath) {
#Write-Host "DEBUG Info - Write-LogFile: LogPath was already been set!"
#Write-Host "DEBUG Info - Write-LogFile: LogPath is $LogPath"
} #end elseif
ELSE {
Write-Host "Unexpected Error!"
Write-Host $Error
Return $False
} #end else
IF ($LogPath) {
#After LogPath determination - try to log the string
Try {
Write-Host "$LogLine"
Add-content $LogPath\$LogFile -value "$(Get-Date -Format 'dd.MM.yyyy')-$(Get-Date -Format 'HH:mm:ss'): $LogLine" -ErrorAction Stop
} #end try
Catch {
Write-Host "DEBUG Info - Write-LogFile: Cannot write to LogFile!"
Write-Host "DEBUG Info - Write-LogFile: Exiting, because of this Issue."
Write-Host $Error
EXIT
} #end catch
} #end if
ELSE {
Write-Host "DEBUG Info - Write-LogFile: Cannot write to LogFile!"
Write-Host "DEBUG Info - Write-LogFile: Exiting, because of this Issue."
Write-Host $Error
EXIT
} #end else
} #end function