Jekyll2023-12-03T12:58:15+00:00https://danielschwensen.github.io/feed.xmlMy Personal NotesThis blog serves as a dedicated space for the documentation of my explorations, with particular emphasis on Powershell and AWS, among other topics.
Daniel SchwensenHow to Install Git on Windows2023-12-03T00:00:00+00:002023-12-03T00:00:00+00:00https://danielschwensen.github.io/Install_Git_Windows<p>Download Git: Visit the official Git website at git-scm.com to download the latest version for Windows.</p>
<h1 id="installation-process">Installation Process</h1>
<p><img src="https://danielschwensen.github.io/assets/2023/Git-01.png" alt="Git Bash 1" /></p>
<p><img src="https://danielschwensen.github.io/assets/2023/Git-02.png" alt="Git Bash 2" /></p>
<p><img src="https://danielschwensen.github.io/assets/2023/Git-03.png" alt="Git Bash 3" /></p>
<p><img src="https://danielschwensen.github.io/assets/2023/Git-04.png" alt="Git Bash 4" /></p>
<p><img src="https://danielschwensen.github.io/assets/2023/Git-05.png" alt="Git Bash 5" /></p>
<p><img src="https://danielschwensen.github.io/assets/2023/Git-06.png" alt="Git Bash 6" /></p>
<p><img src="https://danielschwensen.github.io/assets/2023/Git-07.png" alt="Git Bash 7" /></p>
<p><img src="https://danielschwensen.github.io/assets/2023/Git-08.png" alt="Git Bash 8" /></p>
<p><img src="https://danielschwensen.github.io/assets/2023/Git-09.png" alt="Git Bash 9" /></p>
<p><img src="https://danielschwensen.github.io/assets/2023/Git-10.png" alt="Git Bash 10" /></p>
<h1 id="post-installation-steps">Post-Installation Steps</h1>
<p>Verifying the Installation</p>
<ul>
<li>Open your command prompt or Git Bash terminal.</li>
<li>Type git –version and press Enter. If Git is installed correctly, it should display the installed version.</li>
</ul>
<h1 id="configuring-git">Configuring Git</h1>
<ul>
<li>To personalize your Git environment, configure your user name and email using the following commands:
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
</code></pre></div> </div>
</li>
<li>These details will be used in your commits.</li>
</ul>Daniel SchwensenDownload Git: Visit the official Git website at git-scm.com to download the latest version for Windows.How to check on Linux if Docker is running2023-09-27T00:00:00+00:002023-09-27T00:00:00+00:00https://danielschwensen.github.io/How-to-check-if-Docker-is-running<p>Docker is a popular tool for developing and running applications in containers. It can be used on a variety of platforms, including Linux.</p>
<p>There are a few ways to check if Docker is running on Linux.</p>
<ol>
<li>Use the systemctl command.</li>
</ol>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>sudo systemctl status docker
</code></pre></div></div>
<p>If the Docker daemon is running, you will see an output similar to this:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2023-08-03 14:27:03 UTC; 3h 3min ago
Main PID: 1326 (dockerd)
Tasks: 27
Memory: 15.4M
CPU: 8ms
CGroup: /system.slice/docker.service
└─1326 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
</code></pre></div></div>
<ol>
<li>Use the docker info command.</li>
</ol>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>docker info
</code></pre></div></div>
<p>If the Docker daemon is running, you will see an output similar to this:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Client:
Version: 20.10.8
...
Server:
Version: 20.10.8
...
</code></pre></div></div>
<ol>
<li>
<p>Check the process ID of the Docker daemon.</p>
<p>The Docker daemon writes its process ID to the file /var/run/docker.pid. You can check if the Docker daemon is running by checking if this file exists and if the process ID associated with it is running.</p>
</li>
</ol>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>sudo cat /var/run/docker.pid
</code></pre></div></div>
<p>If the Docker daemon is running, you will see a number. If the number is not running, the Docker daemon is not running.</p>Daniel SchwensenDocker is a popular tool for developing and running applications in containers. It can be used on a variety of platforms, including Linux.Using PowerShell PSReadLine2023-08-04T00:00:00+00:002023-08-04T00:00:00+00:00https://danielschwensen.github.io/PowerShell-PSReadLine<p><strong>PSReadLine</strong>, a module that offers improved command-line editing and rich history capabilities.</p>
<h3 id="installation"><strong>Installation</strong></h3>
<p>Before using PSReadLine, you need to have it installed. It often comes pre-installed with Windows 10 and PowerShell Core, but if you need to install it manually:</p>
<div class="language-powershell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">Install-Module</span><span class="w"> </span><span class="nt">-Name</span><span class="w"> </span><span class="nx">PSReadLine</span><span class="w"> </span><span class="nt">-Force</span><span class="w"> </span><span class="nt">-SkipPublisherCheck</span><span class="w">
</span></code></pre></div></div>
<h3 id="basic-features"><strong>Basic Features</strong></h3>
<p><strong>Syntax Highlighting:</strong></p>
<p>Out of the box, PSReadLine offers real-time syntax highlighting, making it easier to spot mistakes as you type.</p>
<p><strong>Keyboard Shortcuts:</strong></p>
<p>PSReadLine comes packed with a variety of shortcuts, similar to those in <code class="language-plaintext highlighter-rouge">bash</code>, that can make navigation and command editing a breeze.</p>
<ul>
<li><strong>Ctrl + Arrow Keys:</strong> Navigate through tokens (words).</li>
<li><strong>Ctrl + A:</strong> Move to the start of the line.</li>
<li><strong>Ctrl + E:</strong> Move to the end of the line.</li>
</ul>
<h3 id="command-history"><strong>Command History</strong></h3>
<p>One of PSReadLine’s most robust features is its enhanced history management.</p>
<p><strong>Persistent History:</strong></p>
<p>Commands are saved across sessions, so you can recall commands you used days or even weeks ago.</p>
<p><strong>Search History:</strong></p>
<p>Easily search through your command history by pressing <strong>Ctrl + R</strong>. Start typing, and PSReadLine will show matches from your history.</p>
<p><img src="https://danielschwensen.github.io/assets/2023/PSReadLine.png" alt="PSReadLine" /></p>
<h3 id="customization"><strong>Customization</strong></h3>
<p>PSReadLine is highly customizable. To edit the settings:</p>
<div class="language-powershell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">notepad.exe</span><span class="w"> </span><span class="bp">$PROFILE</span><span class="w">
</span></code></pre></div></div>
<p>This will open your PowerShell profile in Notepad, where you can customize PSReadLine to your liking.</p>
<p><strong>Prediction Based on History:</strong> You can set PSReadLine to predict the next command based on your history using:</p>
<div class="language-powershell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">Set-PSReadLineOption</span><span class="w"> </span><span class="nt">-PredictionSource</span><span class="w"> </span><span class="nx">History</span><span class="w">
</span></code></pre></div></div>
<p><strong>View Style for Predictions:</strong> You can customize the view of the predictions. For a list view style, use:</p>
<div class="language-powershell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">Set-PSReadLineOption</span><span class="w"> </span><span class="nt">-PredictionViewStyle</span><span class="w"> </span><span class="nx">ListView</span><span class="w">
</span></code></pre></div></div>
<h3 id="mindmap"><strong>MindMap</strong></h3>
<p><img src="https://danielschwensen.github.io/assets/2023/PSReadLineMindmap.png" alt="PSReadLineMindMap" /></p>
<h3 id="conclusion"><strong>Conclusion</strong></h3>
<p><strong>PSReadLine</strong> is a must-have module for anyone regularly working with PowerShell. Its enhanced editing, history capabilities, and customization options can greatly improve your productivity.</p>Daniel SchwensenPSReadLine, a module that offers improved command-line editing and rich history capabilities.How to list the content of a Glacier vault?2023-07-03T00:00:00+00:002023-07-03T00:00:00+00:00https://danielschwensen.github.io/How-to-list-the-content-of-a-Glacier-vault<p>Want a quick look at what’s inside your AWS Glacier vault? Retrieving an inventory is easier than you might think. This post walks you through the simple steps to initiate an inventory-retrieval job and download your vault’s contents.</p>
<p>First you need to initiate a job to retrieve an inventory of a vault.</p>
<p>Here is the general process:</p>
<ol>
<li>Initiate an inventory-retrieval job: You can do this via the AWS Management Console, AWS CLI, or AWS SDKs. The job essentially requests AWS Glacier to compile an inventory of the content of your vault. This job typically takes several hours to complete because Glacier is optimized for infrequent access.</li>
<li>After the job is complete, download the output of the job: The output is a file that contains the inventory of your vault. The inventory includes information about each archive in the vault, such as the archive ID, description, and creation date.
Here’s an example of how to initiate an inventory-retrieval job using the AWS CLI:</li>
</ol>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>aws glacier initiate-job --account-id - --vault-name your_vault_name --job-parameters '{"Type": "inventory-retrieval"}'
</code></pre></div></div>
<p>Replace your_vault_name with the name of your vault.
This command will return a job ID, which you can use to check the status of the job. You can do that with this command:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>aws glacier describe-job --account-id - --vault-name your_vault_name --job-id your_job_id
</code></pre></div></div>
<p>Replace your_vault_name with the name of your vault, and your_job_id with the job ID returned by the initiate-job command.</p>
<p>Once the job is complete, you can download the output of the job with this command:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>aws glacier get-job-output --account-id - --vault-name your_vault_name --job-id your_job_id output_file_name
</code></pre></div></div>
<p>Replace your_vault_name with the name of your vault, your_job_id with the job ID, and output_file_name with the name you want to give to the output file.</p>Daniel SchwensenWant a quick look at what’s inside your AWS Glacier vault? Retrieving an inventory is easier than you might think. This post walks you through the simple steps to initiate an inventory-retrieval job and download your vault’s contents.How to Upload files and folder to AWS s3 with a CLI2023-06-28T00:00:00+00:002023-06-28T00:00:00+00:00https://danielschwensen.github.io/How-to-Upload-files-and-folder-to-AWS-s3-with-a-CLI<p>To upload files and folders to Amazon S3 using the AWS Command Line Interface (CLI), you can use the aws s3 cp command.</p>
<p>Here is the basic syntax for the aws s3 cp command:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>aws s3 cp <local-path> <s3-path>
</code></pre></div></div>
<ul>
<li>local-path is the path to the file or folder on your local machine that you want to upload.</li>
<li>s3-path is the path to the S3 bucket where you want to upload the file or folder. It should be in the format s3://<bucket-name>/<key>, where <bucket-name> is the name of the S3 bucket and <key> is the path to the location within the bucket where you want to upload the file or folder.</key></bucket-name></key></bucket-name></li>
</ul>
<p>For example, to upload a file named file.txt from your local machine to the root of an S3 bucket named my-bucket, you could use the following command:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>aws s3 cp file.txt s3://my-bucket/file.txt
</code></pre></div></div>
<p>To upload a folder named my-folder from your local machine to the root of an S3 bucket, you can use the –recursive flag to copy all the files and subfolders within the folder:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>aws s3 cp my-folder s3://my-bucket/ --recursive
</code></pre></div></div>
<p>You can also specify a different destination folder within the S3 bucket by adding it to the s3-path. For example, to upload the contents of my-folder to a folder named s3-folder within the S3 bucket, you can use the following command:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>aws s3 cp my-folder s3://my-bucket/s3-folder/ --recursive
</code></pre></div></div>
<p>Note that the aws s3 cp command will overwrite any existing files with the same name in the destination.
To avoid overwriting existing files, you can use the –dryrun flag to test the command without actually uploading the files.</p>
<p>You can also use the aws s3 sync command to synchronize a local folder with an S3 bucket or a folder within an S3 bucket.</p>
<p>Here is the basic syntax for the aws s3 sync command:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>aws s3 sync <local-path> <s3-path>
</code></pre></div></div>
<ul>
<li>local-path is the path to the folder on your local machine that you want to sync with the S3 bucket.</li>
<li>s3-path is the path to the S3 bucket or folder where you want to sync the local folder. It should be in the format s3://<bucket-name>/<key>, where <bucket-name> is the name of the S3 bucket and <key> is the path to the location within the bucket where you want to sync the local folder.</key></bucket-name></key></bucket-name></li>
</ul>
<p>For example, to sync the contents of a local folder named my-folder with the root of an S3 bucket named my-bucket, you could use the following command:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>aws s3 sync my-folder s3://my-bucket/
</code></pre></div></div>
<p>To sync the contents of my-folder with a folder named s3-folder within the S3 bucket, you can use the following command:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>aws s3 sync my-folder s3://my-bucket/s3-folder/
</code></pre></div></div>
<p>The aws s3 sync command will copy new and updated files from the local folder to the S3 bucket, and it will also delete any files from the S3 bucket that are not present in the local folder.
You can use the –delete flag to disable this behavior.</p>Daniel SchwensenTo upload files and folders to Amazon S3 using the AWS Command Line Interface (CLI), you can use the aws s3 cp command.How to Create and Manage Python Virtual Environments2023-05-08T00:00:00+00:002023-05-08T00:00:00+00:00https://danielschwensen.github.io/How-to-Create-and-Manage-Python-Virtual-Environments<p>Python is a powerful and versatile programming language, widely used for web development, data analysis, artificial intelligence, and more. As a developer, you might find yourself working on multiple projects with different sets of dependencies and configurations. This is where Python virtual environments come to the rescue! In this blog post, we’ll explore the benefits of using virtual environments, how to create and manage them, and best practices to keep your projects organized and conflict-free.</p>
<p>Why use Python virtual environments?</p>
<p>Python virtual environments are self-contained, isolated environments that allow you to work on different projects without causing conflicts between their respective dependencies, versions, and settings. Some key benefits of using virtual environments include:</p>
<ol>
<li>Project isolation: Maintain separate environments for each project, preventing conflicts and ensuring stability.</li>
<li>Dependency management: Install and manage packages specific to each project without affecting the system-wide Python installation.</li>
<li>Reproducible builds: Easily share your project with others by providing a list of dependencies and their versions, ensuring a consistent development environment.</li>
</ol>
<p>Creating a Python virtual environment:</p>
<p>To create a Python virtual environment, you can use built-in tools like venv (available from Python 3.3 onwards) or third-party packages like virtualenv. In this guide, we’ll focus on using venv.</p>
<p>Follow these steps to create a new virtual environment:</p>
<ol>
<li>Open a terminal (Linux or macOS) or command prompt (Windows).</li>
<li>Navigate to the directory where you want to create the virtual environment.</li>
<li>Run the following command, replacing myenv with your desired environment name:
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>python -m venv myenv
</code></pre></div> </div>
</li>
<li>Activate the virtual environment:
<ul>
<li>On Windows, run:
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>myenv\Scripts\activate
</code></pre></div> </div>
</li>
<li>On Linux or macOS, run:
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>source myenv/bin/activate
</code></pre></div> </div>
<p>After activation, your terminal or command prompt will display the virtual environment’s name in parentheses (e.g., (myenv)). Now, any package you install or changes you make to Python settings will only affect the virtual environment.</p>
</li>
</ul>
</li>
</ol>
<p>Managing packages in a virtual environment:</p>
<p>With your virtual environment activated, you can install packages using pip. For example, to install a specific version of a package, run:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>pip install package_name==version_number
</code></pre></div></div>
<p>To generate a list of installed packages and their versions, run:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>pip freeze > requirements.txt
</code></pre></div></div>
<p>This creates a requirements.txt file, which you can share with others to ensure they have the same development environment.</p>
<p>Deactivating the virtual environment:</p>
<p>When you’re done working on a project, deactivate the virtual environment to return to the system-wide Python installation by running:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>deactivate
</code></pre></div></div>
<p>Conclusion:</p>
<p>Python virtual environments are a powerful tool for managing multiple projects with different dependencies, ensuring a clean and organized development workspace. By mastering virtual environments, you’ll minimize conflicts, streamline dependency management, and improve collaboration with your team.</p>Daniel SchwensenPython is a powerful and versatile programming language, widely used for web development, data analysis, artificial intelligence, and more. As a developer, you might find yourself working on multiple projects with different sets of dependencies and configurations. This is where Python virtual environments come to the rescue! In this blog post, we’ll explore the benefits of using virtual environments, how to create and manage them, and best practices to keep your projects organized and conflict-free.Securely Storing and Using Credentials in PowerShell Scripts2023-03-23T00:00:00+00:002023-03-23T00:00:00+00:00https://danielschwensen.github.io/Securely_Storing_and_Using_Credentials_in_PowerShell_Scripts<p>When working with PowerShell scripts, it’s often necessary to use sensitive information like usernames and passwords to authenticate against various services or systems. However, storing these credentials in plain text is a security risk that should be avoided. In this blog post, we’ll discuss how to securely store and use credentials in PowerShell without exposing them in plain text.</p>
<p>Step 1: Encrypt and Store the Password</p>
<p>The first step is to securely store your password by encrypting it and saving it to a file. PowerShell provides a ConvertFrom-SecureString cmdlet that can be used to save encrypted strings to a file.</p>
<p>Here’s how to do it:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code># Type the password in the console when prompted
$Credential = Get-Credential -UserName 'YourUserName' -Message 'Enter your password'
# Save the encrypted password to a file
$Credential.Password | ConvertFrom-SecureString | Set-Content 'C:\path\to\secure_password.txt'
</code></pre></div></div>
<p>This code snippet prompts you to enter your password in a secure manner and then saves the encrypted password to a file. Keep in mind that this encrypted password can only be decrypted by the same user and on the same computer where it was encrypted.</p>
<p>Step 2: Use the Encrypted Password in Your Script</p>
<p>Once the password is securely stored, you can use it in your PowerShell scripts by reading the encrypted password from the file and creating a PSCredential object.</p>
<p>Here’s an example:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code># Read the encrypted password from the file
$SecurePassword = Get-Content 'C:\path\to\secure_password.txt' | ConvertTo-SecureString
# Create a PSCredential object using the encrypted password
$UserName = 'YourUserName'
$Credential = New-Object System.Management.Automation.PSCredential($UserName, $SecurePassword)
# Use the credential object in your script, e.g., for a remote session
$Session = New-PSSession -ComputerName 'RemoteComputerName' -Credential $Credential
Invoke-Command -Session $Session -ScriptBlock { # Your commands here }
</code></pre></div></div>
<p>In this code snippet, we read the encrypted password from the file, create a PSCredential object, and then use that object for authentication. This ensures that the password is not exposed in plain text within the script.</p>
<p>Conclusion</p>
<p>By encrypting and securely storing sensitive information like passwords, you can minimize the risk of unauthorized access to your scripts and systems. Using the PSCredential object and related cmdlets in PowerShell makes it easy to manage and use credentials without exposing them in plain text.</p>Daniel SchwensenWhen working with PowerShell scripts, it’s often necessary to use sensitive information like usernames and passwords to authenticate against various services or systems. However, storing these credentials in plain text is a security risk that should be avoided. In this blog post, we’ll discuss how to securely store and use credentials in PowerShell without exposing them in plain text.grep2023-02-26T00:00:00+00:002023-02-26T00:00:00+00:00https://danielschwensen.github.io/grep<p>The grep command in Linux is a powerful tool for searching and filtering text. It stands for “global regular expression print,” and it allows you to search for patterns within text files and output the matching lines.</p>
<p>Basic syntax</p>
<p>The basic syntax for using grep is as follows:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>grep [options] pattern [files]
</code></pre></div></div>
<p>Here is a breakdown of the components:</p>
<ul>
<li>options: optional flags that control the behavior of grep</li>
<li>pattern: the regular expression pattern to search for</li>
<li>files: a list of one or more files to search; if not specified, grep will search through the standard input</li>
</ul>
<p>Regular expressions</p>
<p>A regular expression is a sequence of characters that defines a search pattern. Regular expressions can include special characters and metacharacters that allow you to match patterns more flexibly. Some common metacharacters include:</p>
<ul>
<li>.: matches any single character</li>
<li>*: matches zero or more of the preceding character or expression</li>
<li><code class="language-plaintext highlighter-rouge">[]</code>: matches any single character within the brackets</li>
<li>^: negates the character set if it appears as the first character within the brackets</li>
<li><code class="language-plaintext highlighter-rouge">[:alnum:]</code>: matches any alphanumeric character</li>
<li><code class="language-plaintext highlighter-rouge">[:alpha:]</code>: matches any alphabetic character</li>
<li><code class="language-plaintext highlighter-rouge">[:digit:]</code>: matches any digit</li>
<li><code class="language-plaintext highlighter-rouge">[:lower:]</code>: matches any lowercase character</li>
<li><code class="language-plaintext highlighter-rouge">[:upper:]</code>: matches any uppercase character</li>
</ul>
<p>Here are some examples of regular expressions:</p>
<ul>
<li>abc: matches the string “abc”</li>
<li>a.c: matches any string that begins with “a” and ends with “c” with any character in between</li>
<li>a*c: matches any string that begins with “a” and ends with “c” with zero or more “a” characters in between</li>
<li><code class="language-plaintext highlighter-rouge">[abc]</code>: matches any string that contains “a”, “b”, or “c”</li>
<li><code class="language-plaintext highlighter-rouge">[^abc]</code>: matches any string that does not contain “a”, “b”, or “c”</li>
</ul>
<p>Options</p>
<p>grep has many options that allow you to control its behavior. Here are some common ones:</p>
<ul>
<li>-i: ignores case</li>
<li>-v: inverts the match, showing lines that do not match the pattern</li>
<li>-c: counts the number of matches</li>
<li>-n: shows the line numbers of the matches</li>
<li>-E: treats the pattern as an extended regular expression</li>
<li>-w: matches the pattern as a whole word</li>
<li>-o: shows only the matched part of the line</li>
</ul>
<p>Examples</p>
<p>Here are some examples of using grep:</p>
<ul>
<li>grep “hello” file.txt: searches for the string “hello” in the file “file.txt”</li>
<li>grep -i “hello” file.txt: searches for the string “hello” (ignoring case) in the file “file.txt”</li>
<li>grep -v “hello” file.txt: searches for lines that do not contain the string “hello” in the file “file.txt”</li>
<li>grep -c “hello” file.txt: counts the number of lines that contain the string “hello” in the file “file.</li>
</ul>Daniel SchwensenThe grep command in Linux is a powerful tool for searching and filtering text. It stands for “global regular expression print,” and it allows you to search for patterns within text files and output the matching lines.Parse command line tool output with Powershell2023-01-29T00:00:00+00:002023-01-29T00:00:00+00:00https://danielschwensen.github.io/Parse-command-line-tool-output-with-Powershell<p>The other day, I had to restart a Windows server using only Powershell. The catch was that I had to do it only if the output of a command line tool was a certain way. I also kept a log for documentation purposes.</p>
<p>This script shows how easy it was.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$oascmd = "C:\path\to\oascmd.exe"
$output = & $oascmd
# Check if the output contains the string "alive"
if($output -match "alive") {
Restart-Service -Name "Station_xl" -ErrorAction SilentlyContinue
$time = Get-Date -Format "MM/dd/yyyy HH:mm:ss"
$log = "$time - Service Station_xl stopped and started. Output: $output"
Add-Content -Path "C:\path\to\logfile.txt" -Value $log
}
</code></pre></div></div>
<p>With this script, you can check the status of a service and restart it automatically if needed. This saves you time and effort.</p>Daniel SchwensenThe other day, I had to restart a Windows server using only Powershell. The catch was that I had to do it only if the output of a command line tool was a certain way. I also kept a log for documentation purposes.Stack vs Heap in C#2022-12-20T00:00:00+00:002022-12-20T00:00:00+00:00https://danielschwensen.github.io/C_Sharp-stack-vs-heap<p>In C#, there are two main ways to store data in memory: on the stack and on the heap.</p>
<h2 id="the-stack">The Stack</h2>
<p>The stack is a memory region used to store local variables and function call data. It is a LIFO (last-in, first-out) structure, meaning that the last item placed on the stack is the first one to be removed. When a function is called, its local variables and any other data it needs are pushed onto the stack. When the function returns, this data is popped off the stack.</p>
<p>One of the main advantages of the stack is that it is very fast to allocate and deallocate memory, as data is simply pushed and popped off the top of the stack. This makes the stack a good choice for storing temporary data that is used and discarded quickly.</p>
<p>However, the stack has a fixed size, and if a program tries to push more data onto the stack than it has room for, it will result in a stack overflow. This can cause the program to crash or behave unexpectedly.</p>
<h2 id="the-heap">The Heap</h2>
<p>The heap is a memory region used to store dynamically-allocated objects. It is much larger than the stack and has no fixed size, so it can grow or shrink as needed. When an object is created on the heap, it is given a memory address that can be used to access it.</p>
<p>One of the main advantages of the heap is that it allows objects to have a longer lifetime than objects stored on the stack. When an object is stored on the stack, it is automatically deallocated when the function that created it returns. However, an object on the heap can exist for as long as it is needed, as it is not tied to any particular function call.</p>
<p>The heap is also useful for storing objects that need to be shared among multiple functions or threads. However, allocating and deallocating memory on the heap is generally slower than on the stack, as it requires more complex operations.</p>
<h2 id="choosing-between-the-stack-and-the-heap">Choosing Between the Stack and the Heap</h2>
<p>In general, it is best to store temporary data on the stack and long-lived objects on the heap. This can help to improve the performance of your program, as the stack is faster and the heap is more flexible.</p>
<p>However, there are some situations where it is necessary to use the heap even for temporary data. For example, if you need to store a large array or a struct with many fields, it may not fit on the stack. In these cases, it is necessary to allocate the data on the heap.</p>
<p>It is also possible to manually control the location of an object in memory using the fixed keyword in C#. This allows you to specify that an object should be stored on the stack or the heap, regardless of its default behavior. However, this should generally be avoided unless absolutely necessary, as it can be difficult to use correctly and can have unintended consequences.</p>
<p>In summary, understanding the difference between the stack and the heap is important for any C# programmer. The stack is faster but has a fixed size, while the heap is more flexible but slower. Choosing the right location to store your data can have significant impacts on the performance and behavior of your program.</p>Daniel SchwensenIn C#, there are two main ways to store data in memory: on the stack and on the heap.